• I’m on a single post page trying to display posts from the same child taxonomy as the current post. Currently I’m getting the posts from the parent taxonomy as well, but I only want the ones from the same CHILD taxonomy.

    This is what I got this far:

    <?php
    
    				//Get array of terms
    				$terms = get_the_terms( $post->ID , 'destinations', 'string');
    				//Pluck out the IDs to get an array of IDS
    				$term_ids = wp_list_pluck($terms,'term_id');
    
    				//Query posts with tax_query. Choose in 'IN' if want to query posts with any of the terms
    				//Chose 'AND' if you want to query for posts with all terms
    				  $second_query = new WP_Query( array(
    				      'post_type' => 'sites',
    				      'tax_query' => array(
    				                    array(
    				                        'taxonomy' => 'destinations' ,
    				                        'field' => 'id',
    				                        'terms' => $term_ids,
    				                        'operator'=> 'IN' //Or 'AND' or 'NOT IN'
    
    				                     )),
    				      'posts_per_page' => 999,
    				      'ignore_sticky_posts' => 1,
    				      'orderby' => 'rand',
    				      'post__not_in'=>array($post->ID)
    				   ) );
    
    				//Loop through posts and display...
    				    if($second_query->have_posts()) { ?>
    
    						<div class="onefourthscol"><div id="related-sites">
    
    		<div id="related-header"><h2>Related posts
    			</h2></div>
    
    				 <?php    while ($second_query->have_posts() ) : $second_query->the_post(); ?>
    
    				 	//MY POST CONTENT HERE//
    
    				           <br class="clearboth" />
    
    				     </div></a>
    
    				   <?php endwhile; wp_reset_query(); ?>
    
    			<br class="clearboth" />
    
    </div></div>
    
     <?php  }
    
    			?>

    The operator AND function doesn’t work, I get no posts at wall while changing that one.

    Grateful for help!

The topic ‘Display posts from the same child taxonomy’ is closed to new replies.