Support » Fixing WordPress » exclude posts with certain tag to display twice on multiple loop

  • Resolved soeezauto

    (@soeezauto)


    I am new to this and just hit (another) wall.

    By the way, this is an integration of WP to my website, not a full installation, though I do not believe it matters in this case.

    I each one of my web pages, I want to have, say, 5 call to posts. Firstly I want to show posts with tags related to that page and then, if
    I do not have all the 5 posts, complete with the most recent, for instance.

    Of course, if any of the ‘related’ posts are in the ‘most recent’ they should not show.

    So, I have both portions working, but posts from ‘related’ are being repeated on ‘most recent’.

    Appreciate any help.

    <?php
    $tag = $brand;
    
        $args=array(
          'tag' => $tag,
          'showposts'=>5
        );
        $my_query = new WP_Query($args);
    
        if( $my_query->have_posts() ) {
    $n = $my_query->post_count;
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <div class="news_call">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><h3><?php the_title(); ?></h3></a>
    
    	<div class="storycontent">
    		<?php the_content(__('(lire la suite...)')); ?>
    	</div>
    </div>
    <?php
          endwhile;
        }
    if(($n < 5)){
    	global $post;
    $args = array( 'posts_per_page' => 5-$n,
    	       'tag__not__in' => array($tag)
    	       );
        $my_query = new WP_Query($args);
         if( $my_query->have_posts() ) {
    
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <div class="news_call">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><h3><?php the_title(); ?></h3></a>
    
    	<div class="storycontent">
    		<?php the_content(__('(lire la suite...)')); ?>
    	</div>
    </div>
    <?php endwhile;
    }
        }
      wp_reset_query();
    
    ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello,

    I believe the correct parameter is tag__not_in (one less underscore than in your code) and it is expecting an array of tag IDs, but it appears that you’re passing the tag slug.

    Thanks,
    Lucas

    Hello karpstrucking, thanks for your reply.

    In the meantime I was pursuing another ways to do this and found a tip
    at StackOverflow (lost the link since them ).

    I actually did not try your method, but it seems in line with what I found in the end.

    So, I put the post ID’s from the first loop in an array and then, in the second loop, just skipped the posts whose ID were on the ID array.

    Thanks again!

    <?php
    $tag = $brand;
    
        $args=array(
          'tag' => $tag,
          'showposts'=>5
        );
        $my_query = new WP_Query($args);
    $id=array();
        if( $my_query->have_posts() ) {
          echo '<h2>Articles liés à '.$tag.':</h2>';
    $nct = $my_query->post_count;
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <div class="news_call">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><h3><?php the_title(); ?></h3></a>
    
    	<div class="storycontent">
    		<?php the_content(__('(lire la suite...)')); ?>
    	</div>
    </div>
    <?php
    $ids[]= $post->ID;//**** GOT THE ID's HERE****
          endwhile;
        }
    if((!isset($nct) or $nct < 5)){
    (isset($nct) ? $n_posts = 6-$nct : $n_posts=5);
    	global $post;
    $args = array( 'posts_per_page' => $n_posts,
    	       );
        $my_query = new WP_Query($args);
         if( $my_query->have_posts() ) {
          echo '<h2>Articles récents:</h2>';
    
          while ($my_query->have_posts()) : $my_query->the_post();
         if ( !isset($ids) or !in_array($post->ID, $ids)){ //*** EXCLUDED POSTS WITH ID SAME AS IN ID ARRAY****
          ?>
    <div class="news_call">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><h3><?php the_title(); ?></h3></a>
    
    	<div class="storycontent">
    		<?php the_content(__('(lire la suite...)')); ?>
    	</div>
    </div>
    <?php }endwhile;
    }
        }
      wp_reset_query();  // Restore global post data stomped by the_post().
    
    ?>

    Just to mark the issue as resolved.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘exclude posts with certain tag to display twice on multiple loop’ is closed to new replies.