Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Possible Duplicate:
Duplicate posts

I'm trying to create a home page, in which there is a top posts section that displays the three most recent posts (for all categories), and category subsections that display the most recent posts for that category only.

I know how to get that far, but I'm not sure how to make sure there isn't any overlap (I don't want any posts to show up both at the top and in the category section of the page). Is there an efficient way to do this?

share|improve this question
    
Another: wordpress.stackexchange.com/questions/50677/… Please use the search feature before posting a question. :) –  Brian Fegter Sep 14 '12 at 4:08
    
Sorry, I searched for a solution before, but didn't find this. Very similar question. Thx! –  a_user Sep 14 '12 at 18:12
add comment

marked as duplicate by Brian Fegter, kaiser, Rarst Oct 2 '12 at 21:09

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 0 down vote accepted

Well, you're going to have a lot of loops and a lot of custom WP_Query objects.

To start, create an $all_posts variable before you start the main loop on your home page. You should likely put all this in your front-page.php template.

<?php
$all_posts = array();
while(have_posts()): the_post();
    $all_posts[] = $post->ID;
    // do stuff with the main loop here
endwhile;

On each subsequent loop, use the $all_posts variable to exclude posts that already happened. Say you want to create loops for the categories with the term_id's 1, 2, and 3:

<?php
foreach(array(1, 2, 3) as $cat))
{
    $cat_query = new WP_Query(array(
        'cat'   => $cat,
        'post__not_in' => $all_posts,
    ));
    if($cat_query->have_posts()
    {
        while($cat_query->have_posts())
        {
            $cat_query->the_post();
            $all_posts[] = $post->ID;
            // display stuff here
        }
    }
}

Just keep appending post IDs to the $all_posts variable to make sure subsequent queries don't include posts that were already seen.

share|improve this answer
    
I had forgotten about the 'post__not_in' parameter. This is what I'm looking for. Thanks! –  a_user Sep 14 '12 at 18:23
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.