Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to avoid duplicate posts in multiple loops? WordPress

  • SOLVED

Here's my site: http://bit.ly/dd6xBX

And here's what I'm trying to accomplish with the middle main column. The first 3 posts are ABC then I have some of my video posts. These video posts are teh result of a totally separate loop. Then after the video posts I created another loop and this loop shows posts ABC and then DEFG, etc. I don't want ABC to show up in this third loop.

There's 3 loops.

Loop 1: ABC
Loop 2: My Video Posts with different css
Loop 3: Same as 1st loop but shows more results. Want to not replicate the posts that already appear in Loop 1.

And here's my code. I know it's a bit much but over the years, I've kept manipulating and manipulating it to fit some of my needs, and even though some of those needs aren't needed, I haven't got a chance to clean it up:



<div id="post-by-date">
<?php
$j = 1;
$postslist = get_posts('showposts=3 && cat=-294,-68,-9,-67');
foreach ($postslist as $post) :
setup_postdata($post);
$image = get_post_meta($post->ID, 'image', $single = true);?>
<?php if($j == 1) : ?>
<div class="middle-news">
<div class="new-wrapper3">
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'recent' ); ?></a>
<?php } ?>
<h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
<small>By <?php the_author_posts_link(); ?> on <?php the_time('F j, Y') ?> at <?php the_time() ?><?php edit_post_link( __('Edit'), ' | ', ''); ?> </small>
<p><?php the_excerpt(); ?></p>
<?php comments_popup_link('No comments yet', '1 comment so far',
'% comments so far (is that a lot?)', 'comments-link', 'Comments are
off for this post'); ?>
</div> <!-- closing div of new-wrapper -->
</div>
<?php elseif ($j == 0) :
$postslist = get_posts('showposts=1&category_name=features');
foreach ($postslist as $post) {
setup_postdata($post);
$image = get_post_meta($post->ID, 'image', $single = true);?>
<div class="middle-news">
<div class="int-wrapper">
<div class="cat-red2">
<?php the_category(' '); ?>
</div>
<h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'interview-home' ); ?></a>
<?php } ?>
<?php the_content_limit(130, ''); ?>
<?php edit_post_link( __('Edit'), ' | ', ''); ?>
</div> <!-- closing div of new-wrapper -->
</div>
<?php } // End loop on the fourth post ?>
<?php elseif ($j == 0) :
$postslist = get_posts('showposts=1&category_name=columns&tag=featured');
foreach ($postslist as $post) {
setup_postdata($post);
$image = get_post_meta($post->ID, 'image', $single = true);?>
<div class="middle-news">
test
<div class="int-wrapper">
<div class="cat-red2">
<?php the_category(' '); ?>
</div>
<h3><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h3>
<?php the_content_limit(170, ''); ?>
by <?php the_author_posts_link(); ?>
<?php edit_post_link( __('Edit'), ' | ', ''); ?>
</div> <!-- closing div of new-wrapper -->
</div>
<?php } // End loop on the 7th post ?>
<?php else : ?>
<div class="middle-news">
<div class="new-wrapper3">
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'recent' ); ?></a>
<?php } ?>
<h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
<small>By <?php the_author_posts_link(); ?> on <?php the_time('F j, Y') ?> at <?php the_time() ?><?php edit_post_link( __('Edit'), ' | ', ''); ?> </small>
<p><?php the_excerpt(); ?></p>
<?php comments_popup_link('No comments yet', '1 comment so far',
'% comments so far (is that a lot?)', 'comments-link', 'Comments are
off for this post'); ?>
</div> <!-- closing div of new-wrapper -->
</div>
<?php endif; $j++;?>
<?php endforeach; ?>

<div id="home-tv">

<a target="_blank" href="http://theurbantwist.tv"><img alt="" src="<?php bloginfo("template_url"); ?>/images/tv-home-head.jpg"/></a>

<?php
$j = 1;
$postslist = get_posts('showposts=4 && cat=68');
foreach ($postslist as $post) :
setup_postdata($post);
$image = get_post_meta($post->ID, 'image', $single = true);
?>


<?php if($j == 1) : ?>
<div class="home-tv-top">


<h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
<div style="color:#aaa;margin-top:0px;"><?php the_excerpt(); ?></div>


</div>
<?php else : ?>

<div id="home-tv-bottom">
<ul> <!-- Entertainment -->

<li>
<h3><a href="<?php the_permalink(); ?>"><img alt="" src="<?php bloginfo("template_url"); ?>/images/small-overlay.png"/></a></h3>

<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'more-tv' ); ?></a>
<?php } ?>

<a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>


</li>

</ul>
</div>
<!-- closing div of new-wrapper -->


<?php endif; $j++;?>
<?php endforeach; ?>


</div><!-- End TV -->

<!--News Pick Up -->

<?php
$j = 1;
$postslist = get_posts('showposts=10 && cat=-294,-68,-9,-67');
foreach ($postslist as $post) :
setup_postdata($post);
$image = get_post_meta($post->ID, 'image', $single = true);?>
<?php if($j == 1) : ?>
<div class="middle-news">
<div class="new-wrapper3">
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'recent' ); ?></a>
<?php } ?>
<h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
<small>By <?php the_author_posts_link(); ?> on <?php the_time('F j, Y') ?> at <?php the_time() ?><?php edit_post_link( __('Edit'), ' | ', ''); ?> </small>
<p><?php the_excerpt(); ?></p>
<?php comments_popup_link('No comments yet', '1 comment so far',
'% comments so far (is that a lot?)', 'comments-link', 'Comments are
off for this post'); ?>
</div> <!-- closing div of new-wrapper -->
</div>
<?php elseif ($j == 0) :
$postslist = get_posts('showposts=1&category_name=features');
foreach ($postslist as $post) {
setup_postdata($post);
$image = get_post_meta($post->ID, 'image', $single = true);?>
<div class="middle-news">
<div class="int-wrapper">
<div class="cat-red2">
<?php the_category(' '); ?>
</div>
<h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'interview-home' ); ?></a>
<?php } ?>
<?php the_content_limit(130, ''); ?>
<?php edit_post_link( __('Edit'), ' | ', ''); ?>
</div> <!-- closing div of new-wrapper -->
</div>
<?php } // End loop on the fourth post ?>
<?php elseif ($j == 0) :
$postslist = get_posts('showposts=1&category_name=columns&tag=featured');
foreach ($postslist as $post) {
setup_postdata($post);
$image = get_post_meta($post->ID, 'image', $single = true);?>
<div class="middle-news">
test
<div class="int-wrapper">
<div class="cat-red2">
<?php the_category(' '); ?>
</div>
<h3><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h3>
<?php the_content_limit(170, ''); ?>
by <?php the_author_posts_link(); ?>
<?php edit_post_link( __('Edit'), ' | ', ''); ?>
</div> <!-- closing div of new-wrapper -->
</div>
<?php } // End loop on the 7th post ?>
<?php else : ?>
<div class="middle-news">
<div class="new-wrapper3">
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'recent' ); ?></a>
<?php } ?>
<h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
<small>By <?php the_author_posts_link(); ?> on <?php the_time('F j, Y') ?> at <?php the_time() ?><?php edit_post_link( __('Edit'), ' | ', ''); ?> </small>
<p><?php the_excerpt(); ?></p>
<?php comments_popup_link('No comments yet', '1 comment so far',
'% comments so far (is that a lot?)', 'comments-link', 'Comments are
off for this post'); ?>
</div> <!-- closing div of new-wrapper -->
</div>
<?php endif; $j++;?>
<?php endforeach; ?>








</div><!-- end #post-by-date -->



Any help is greatly appreciated.

Extra note: I tried Googling answer and tried this approach that a lot of results showed and it didn't work.

http://www.wprecipes.com/how-to-use-two-or-more-loops-without-duplicate-posts

Answers (4)

2012-01-14

John Cotton answers:

<blockquote>I know it's a bit much but over the years, I've kept manipulating and manipulating it to fit some of my needs, and even though some of those needs aren't needed, I haven't got a chance to clean it up</blockquote>

Do it! It'll reap it's own rewards....

To answer your question, just grab a copy of the post ids in the first loop


// Before loop
$grab = array();

//In loop
$grab[] = $post->ID;


And then use the exclude parameter for the last get_posts:
args = array('exclude' => $grab);


Brennen Jones comments:

I hate to sound too much of a noob, but I just tried putting code in and it made my page blank. Is there anyway you can show me where in code exactly it goes?

Thanks

And P.S. I think you're right about the code. After I get this going, I'm going to take the time to clean it up and make it tidy again.


John Cotton comments:


<div id="post-by-date">

<?php
$j = 1;
$grab = array();
// ....



$image = get_post_meta($post->ID, 'image', $single = true);
$grab[] = $post->ID;
?>
// ....


and then - I think ?? - all the get_posts after <!--News Pick Up --> need to have the exclude on them. They are more easily written as arrays rather strings, so the first one would be

$postslist = get_posts( array( 'numberposts' => 10, 'exclude' => $grab ) );


John Cotton comments:

"Here, here" to Jurre's comment :)


Brennen Jones comments:

I can respect that. I'm about to test out what you laid out and if it works, I will double it on the spot for you. :-)


John Cotton comments:

I think you can also write the get_posts like this:

$postslist = get_posts( array( 'numberposts' => 10, 'post__not_in' => $grab ) );

which uses the WP_Query values (giving you more flexibility)


Brennen Jones comments:

It worked!


John Cotton comments:

<blockquote>It worked!</blockquote>
Great! Why not tidy up that code by way of celebration? :)


Brennen Jones comments:

I'm definitely thinking about it. :-)


Brennen Jones comments:

Thanks again for your help. I've raised price and voted. :-)


Brennen Jones comments:

One last quick question.

$postslist = get_posts( array( 'numberposts' => 10, 'exclude' => $grab ) );


What would I need to add to exclude certain categories from posting.

The code works flawlessly otherwise. Just need to exclude a couple of categories from the loop.

Thank

2012-01-14

Kannan C answers:

you can use wp_reset_query(); after each loop

2012-01-14

Nilesh shiragave answers:

Hi

use code from this tutorial.

http://www.wprecipes.com/how-to-use-two-or-more-loops-without-duplicate-posts

Thanks

2012-01-14

Jurre Hanema answers:

My advice would be to clean up your code first, and then come back for this question. Or you would have to raise the price, because right now it is really hard to see what's actually going on.


Brennen Jones comments:

I can respect that. I'm about to test out what John Cotton laid out and if it works, I will double it on the spot for him. :-)