Skip to content

Instantly share code, notes, and snippets.

@jeremyfelt
Created July 4, 2012 23:36
Show Gist options
  • Save jeremyfelt/3050095 to your computer and use it in GitHub Desktop.
Save jeremyfelt/3050095 to your computer and use it in GitHub Desktop.
Related posts are related
<?php
$category_ids = wp_get_object_terms( get_the_ID(), 'category', array( 'fields' => 'ids' ) );
$post_tag_ids = wp_get_object_terms( get_the_ID(), 'post_tag', array( 'fields' => 'ids' ) );
$related_query = new WP_Query( array(
'posts_per_page' => 3,
'post__not_in' => array( get_the_ID() ),
'orderby' => 'rand',
'no_found_rows' => true,
'post_type' => 'post',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $category_ids,
'operator' => 'IN',
),
array(
'taxonomy' => 'post_tag',
'field' => 'id',
'terms' => $post_tag_ids,
'operator' => 'IN',
),
),
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment