WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am making a theme that has the main loop in a widget. Also widgetized is a feature slider and a display of four feature post boxes, each with their own loop.

How do I prevent duplicate posts from appearing in my main loop widget when the other feature widgets are also displayed?

I know I have to declare a global variable and assign that variable an array of the posts IDs from the feature widgets I want to exclude from my main loop widget. Below is the sample code from the three widgets.

Feature Slider Loop:

<?php global $themename_do_not_duplicate;
$themename_do_not_duplicate = array();
$arr = array();
$width = $instance['ImgWidth'];
$height = $instance['ImgHeight'];
$cat_posts = new WP_Query("showposts=" . $instance["featuredNum"] . "&cat=" .       $instance["cat"]);
while ($cat_posts->have_posts()) : $cat_posts->the_post() $themename_do_not_duplicate[] = $posts->ID;?>
            <li><a href="<?php the_permalink();?>">
            $post_title = get_the_title();
            $thumbnail = get_thumbnail($width,$height,'thumb',$post_title,$post_title);
            $thumb = $thumbnail["thumb"];
            print_thumbnail($thumb, $thumbnail["use_timthumb"], $post_title, $width, $height, 'thumb'); ?></a>
            <div class="title">
            <h2><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h2>
    <?php endwhile; wp_reset_query();   ?>

A loop from another Feature loop - this widget actually has four loops like the one below:

<?php query_posts("showposts=1&cat=".get_catId($instance['catOption4']));

              while (have_posts()) : the_post(); $themename_do_not_duplicate[] = $post->ID;?>

        <?php endwhile; wp_reset_query(); ?>

The main loop widget

<?php global $themename_do_not_duplicate;

 if ($instance['NoRepeatPost'] == 1)   {$arrgs=array('posts_per_page'=>$instance['postNum'],'post__not_in' => $themename_do_not_duplicate,'paged'=>$paged,);}  else {$arrgs=array('posts_per_page'=>$instance['postNum'],'paged'=>$paged,);}

if(have_posts()) : while (have_posts()) : the_post();$themename_do_not_duplicate[] = $post->ID; //begin the loop ?> stuff  <?php endif;wp_reset_query(); //and so ends the loop ?>

I have put the global $themename_do_not_duplicate in the header. What do do, oh what to do?

share|improve this question

You should use post__not_in parameter (see Post & Page Parameters in Codex). Since it takes an array you will also need to switch to array rather than string for passing arguments.

Something like this:

    'showposts' => 1,
    'cat' => get_catId( $instance['catOption4'] ),
    'post__not_in' => $themename_do_not_duplicate,
share|improve this answer
I have that in my main loop. The trouble I am having is that I can't pass the value of the $themename_do_not_duplicate variable from the feature loop onto the $themename_do_not_duplicate variable in the main loop. – user3210 Feb 18 '11 at 6:52
@user3210 then it's likely you have some kind of variable scope issues. Try checking with isset() if you have variable properly available where you are trying to use it. – Rarst Feb 18 '11 at 17:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.