I am working on a loop where I want the content (from 2 separate categories) to be displayed like this:

1 large format post, 1 random product, 2 small format posts, 1 feature post ...repeat.

In order to achieve this I have written a template for the whole page - with a parent loop (yet to be written), then 4 templates for each nested loop ( - large/random/small/feature). The loop templates are loaded into the parent page using include(locate_template('looptemplate.php')); - I need to use this method so I can pass a variable ($do_not_duplicate) to each loop.

My problem is that I cannot get numberposts to reset after each loop template, so if the large-format template uses the argument numberposts => 1 then in order to make the small-format template show 2 posts it needs to use numberposts => 3. This is fine for the first iteration of the parent loop, but it is not repeatable.

Here is the code for the parent page template (NB I have not yet written the parent loop because I'm still trying to solve this problem with numberposts):

<?php $do_not_duplicate = array(); ?>

<div id="primary" class="content-area">

<div id="content" class="site-content" role="main">

<div class="largenews">

<?php include(locate_template('largenews.php')); ?>

</div><div class="product">

<?php include(locate_template('productflash.php')); ?>

</div><div class="smallnews">

<?php include(locate_template('smallnews.php')); ?>

</div><div class="featurepost">

<?php include(locate_template('featurepost.php')); ?>


</div><!-- #content -->

And here is the code for largenews.php. (smallnews.php is very similar but want to show 2 more posts)

<?php $args = array( 'numberposts' => 1, 'category' => '35,1052');
    $largepost = get_posts($args);
    foreach( $largepost as $post ) : setup_postdata($post);
        if (in_array($post->ID, $do_not_duplicate)) continue;
        array_push($do_not_duplicate, $post->ID);

//Code to format post

wp_reset_postdata(); ?>

Now I have already tried every combination of numberposts, posts_per_page, showposts, rewind_posts(), wp_reset_postdata, and wp_reset_query that I can imagine, but the numberposts from the first loop is still being counted in the numberposts of the second loop (and any further iterations that will appear).

I have also tried to use $largepost->wp_reset_postdata(); but I get this : "Fatal error: Call to a member function wp_reset_postdata() on a non-object".

Apologies for the length of this question but I hope I have made my problem clear, and included all the relevant info. Apologies to for the wonky code formatting, I couldn't get it to work any other way!

  • I think you want to exclude the post IDs from the query via post__not_in, not from the loop. or maybe I have no clue what you're trying to do here.
    – Milo
    Aug 17, 2015 at 15:19
  • No, it's not the duplicate posts I can't work out, it's the number of posts which each mini-loop displays. I want the "large post" loop to display one post, and the "small post" loop to display two, but it seems that 'numberposts' => 2 argument in the "small post" loop is still affected by the 'numberposts' => 1 argument of the "large post", in spite of having included wp_reset_postdata(); at the end of each loop.
    – GBin
    Aug 17, 2015 at 15:54
  • with this line: if (in_array($post->ID, $do_not_duplicate)) continue;, you appear to be skipping display of these posts in the loop. So in that case, if you don't exclude duplicate posts from the query, you need to query for more posts than end up being displayed, to get past the posts you're skipping. If you exclude duplicate posts from the query, every post you get back is for display, you would remove the duplicate check there, and just add post IDs to the array you're excluding from the next query.
    – Milo
    Aug 17, 2015 at 16:02
  • Oh I see! I would never have worked that out. I think I'd read somewhere that if (in_array($post->ID, $do_not_duplicate)) continue; could mess up the pagination, but I did not understand the significance. So I just removed that line, and inserted 'post__not_in' => $do_not_duplicate into my query. Thanks a million @Milo .
    – GBin
    Aug 17, 2015 at 17:34

1 Answer 1


@Milo's answer worked perfectly (see comment above).

if (in_array($post->ID, $do_not_duplicate)) continue; meant that duplicate posts were still being counted in the loop, though not being displayed.

To fix, remove this line, and exclude duplicate posts from the query with this argument: 'post__not_in' => $do_not_duplicate.

Thank you @Milo for your swift and succinct response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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