Get Related Posts by Tag/Taxonomy Term in WordPress
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
$taxonomy = 'post_tags'; // or whatever you want $query = NULL; $terms = wp_get_post_terms($post->ID, $taxonomy); if ($terms) { // Get the term ids into an array for the query args $term_ids = array(); foreach($terms as $individual_term) $term_ids[] = $individual_term->term_id; $args=array( 'post_type' => get_post_type(), 'tax_query' => array( array( 'taxonomy' => $taxonomy, 'field' => 'id', 'terms' => $term_ids, 'operator' => 'IN' ) ), 'post__not_in' => array($post->ID), 'showposts' => $count, // Number of related posts that will be shown. 'caller_get_posts' => 1 ); $query = WP_Query($args) } |
