WordPress.org

Ready to get started?Download WordPress

Forums

Exclude posts from first loop in second loop (6 posts)

  1. kesp
    Member
    Posted 5 months ago #

    Hi,

    I've created a related posts function for the end of my single.php that has 2 loops. The first loop displays related posts by tag and then if there's not enough of those then the second loop kicks in and displays some recent posts from the same category.

    The idea is that i always want to show 4 related posts but i haven't always got enough related by tag so when that happens i display a few from the same category to pad it out.

    Both loops exclude the current post id, however, the problem i have is that it is possible for a post to be returned in the first tag loop and the second by-category loop, so i end up with 2 of the same in the related posts

    What I want to do is exclude any posts returned in the first by-tag loop from the second by-category loop.

    I've been trying to implement something this solution but not much luck

    http://stackoverflow.com/questions/12539952/two-loops-exclude-post-from-first-loop-in-second-loop

    Here's my code:

    <?php
            $orig_post = $post;
            global $post;
            $tags = wp_get_post_tags($post->ID);
             $max_articles = 4;  // How many articles to display
    		 $cnt = 0;
    
            if ($tags) {
           	 $tag_ids = array();
           	 foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
           	 $args=array(
            'tag__in' => $tag_ids,
            'post__not_in' => array($post->ID),
            'posts_per_page'=> $max_articles, // Number of related posts to display.
            'caller_get_posts'=>1 ,
    		'tag__not_in' => array('480'),
    		'orderby' => 'date',
    		'order' => 'DESC'
    
            );  
    
            $my_query = new wp_query( $args );
    	$exclude_featured = array();
    
            while( $my_query->have_posts() ) {
            $my_query->the_post();
    		$cnt++;
    		$exclude_featured = $post->ID;
    
            ?> <?php the_title(); ?>  	
    
            <? }
            }
            $post = $orig_post;  
    
             if ($cnt < $max_articles) {
    		 			 $args=array(
    		'category_name' => $category[0]->cat_name,
            'post__not_in' =>  array($post->ID),
            'caller_get_posts'=>1
            );  
    
    				 			 $blah=array(
            'post__not_in' =>  array($exclude_featured)
            );
           $mergeblah = array_merge( $args, $blah );
            $relQuery = new WP_query( $mergeblah );  
    
    		if($relQuery->have_posts()) : 
    
    		while($relQuery->have_posts()) : $relQuery->the_post();
    		            $cnt++;
                    if ($cnt > $max_articles) break;
    		?>
    		        <?php the_title(); ?>
    		<?php
    		endwhile;
    
    		else :
    			//do nothing
    		endif;
    
    		 }
    		wp_reset_postdata();
            ?>

    Any pointers much appreciated.

    Thanks,

    Kes

  2. alchymyth
    Forum Moderator
    Posted 5 months ago #

    the Codex has some examples:
    https://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action

    start with changing this line:
    $exclude_featured = $post->ID;

    to:
    $exclude_featured[] = $post->ID;

  3. kesp
    Member
    Posted 5 months ago #

    Hi alchymyth,

    Thanks for the reply.

    I just gave that a try but no change to the output..

    Kes

  4. bcworkz
    Member
    Posted 5 months ago #

    When you define the fill in query and array merge the existing related posts, the merge overwrites the initial value of the presumably main post ID. Except it is not the main post ID at this point, it is the last post in the related loop. To include the main post in the exclusion array, it's probably easiest to include the ID when the exclusion array is first initialized. In other words, instead of:
    $exclude_featured = array();
    do this:
    $exclude_featured = array( $post->ID );

    Then get rid of the array merge, define 'post__not_in' => $exclude_featured straight away. Do not place it in an array, it is an array already. I don't think 'post__not_in' can deal with nested arrays.

    If you still have trouble, repost your latest code and describe exactly how it is not working right.

  5. kesp
    Member
    Posted 5 months ago #

    Hey bc,

    That works a treat - thanks!

    So essentially, we've grabbed the current post ID, put it in the array exclude_featured and then inserted the post ID's from the first loop in exclude_featured as well.

    Then in the second loop exclude everything in exclude_featured

    Have I got that right?

    Cheers,

    Kes

  6. bcworkz
    Member
    Posted 5 months ago #

    Exactly! You had the right idea all along, it's just that you weren't collecting the IDs in a sustainable manner.

Reply

You must log in to post.

About this Topic