Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
If the asker does not get an answer then they have 10 days to request a refund.
$10
How to avoid duplicate posts in multiple loops?
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
This question has been answered.
Brennen Jones | 01/14/12 at 9:53am
Edit
Previous versions of this question:
01/14/12 at 10:30am
The experts have suggested, on average, a prize of $15 for this question.
(4) Possible Answers Submitted...
See a chronological view of answers?
Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
-

Last edited:
01/14/12
10:01amKannan C says:you can use
after each loopwp_reset_query();Previous versions of this answer: 01/14/12 at 10:01am
-

Last edited:
01/14/12
9:58amNilesh shiragave says:Hi
use code from this tutorial.
http://www.wprecipes.com/how-to-use-two-or-more-loops-without-duplicate-posts
Thanks -

Last edited:
01/14/12
10:00amJohn Cotton says: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
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);
- 01/14/12 10:07am
Brennen Jones says: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. - 01/14/12 10:18am
John Cotton says:
<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 ) );
- 01/14/12 10:19am
John Cotton says:"Here, here" to Jurre's comment :)
- 01/14/12 10:23am
Brennen Jones says: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. :-)
- 01/14/12 10:27am
John Cotton says: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) - 01/14/12 10:27am
Brennen Jones says:It worked!
- 01/14/12 10:28am
John Cotton says:It worked!
Great! Why not tidy up that code by way of celebration? :) - 01/14/12 10:32am
Brennen Jones says:I'm definitely thinking about it. :-)
- 01/14/12 10:35am
Brennen Jones says:Thanks again for your help. I've raised price and voted. :-)
- 01/14/12 10:46am
Brennen Jones says: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
- 01/14/12 10:07am
-

Last edited:
01/14/12
10:13amJurre Hanema says: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.
- 01/14/12 10:22am
Brennen Jones says: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. :-)
- 01/14/12 10:22am
This question has expired.
Brennen Jones, Sébastien | French WordpressDesigner, Julio Potier, Francisco Javier Carazo Gil voted on this question.
Current status of this question: Completed
Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
If the asker does not get an answer then they have 10 days to request a refund.
