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?


1 Answer 1


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.

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

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:

foreach(array(1, 2, 3) as $cat))
    $cat_query = new WP_Query(array(
        'cat'   => $cat,
        'post__not_in' => $all_posts,
            $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.

  • I had forgotten about the 'post__not_in' parameter. This is what I'm looking for. Thanks!
    – a_user
    Sep 14, 2012 at 18:23

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