Support » Fixing WordPress » 2 loops not behaving. Count is off

  • Resolved Rev. Voodoo


    There’s a paste of the code I’m working with. The one thing I’ve never done in WP until now, is work with multiple queries. I’ve exhausted my knowledge, and reading got me this far…. but I’m stuck.

    There’s 2 loops. The first loop should show either a sticky post if it exists, or the most recent post if no sticky. Only 1 post should show.

    Notice the query for the second loop is inside a conditional. The second loop should show 9 posts, excluding stickies, excluding the post that appears in loop 1, on the home page. Outside of the home page, the query should not apply, so I should just get my normal 10 posts.

    I’m almost there, but my count is off. EVen though I’m asking for 1+9 posts on the home page, I’m getting 1+7. The rest of the behaviour is correct as far as handling stickies and not duplicating.

    On the subsequent paginated pages I’m getting 9 posts when I want 10.

    Any idea what is throwing off my post count? I need to get it reliable and specific for my new layout.

    Is it due to excluding the stickies and dupes that throws the count off, if so, how can I account for this?

    Thanks in advance! (I know the code isn’t pretty. I hack things together as I read, and once everything works, I’ll go back through and make it nice and pretty)

Viewing 15 replies - 1 through 15 (of 19 total)
  • if you’re not on the first page, there is no actual query set for <?php while ($my_query_2-> have_posts() ) :

    possibly needs an ‘else’ section to set the query.

    also, you are checking <?php if ( have_posts() ) : ?> where it possibly should be <?php if ( $my_query_2->have_posts() ) : ?>

    weird why the loop would show post at all (?)
    have you checked if the shown posts are the ones you would expect?
    have you checked that the settings – reading is set to show 10 posts?

    (btw, the ‘do_not_duplicate’ would maximal jump one post.)

    Moderator keesiemeijer


    Try if this works [untested]:

    Wow, I’m a bonehead. That above link is not the right code. That was me working through a thought process I never finished. Here is the code I was having a problem with. Its similar to the above, just using query_posts on the second loop

    @keesiemeijer The code you gave works exactly as my original code does (in that second post, not my original)

    I get 1+7 posts on the fron page

    I get 9 posts on subsequent pages

    I have checked WP settings, set to 10 posts.

    This is on a brand new localhost 3.3 install, absolutely nothing added other than the test unit for content

    Moderator keesiemeijer


    And this?: [also untested]:
    (A little alteration of the first pastebin)

    @keesiemeijer That pastebin got me 1+7 on home, and 8 on subsequent

    Wish I understood queries just a bit better so I could understand what throws tese numbers off.

    Am I approaching this right? Is there a better way to get what I’m after? I’m not set on what I”m doing at all, it’s just what I thought of after codexing/googling for a couple hours

    Moderator keesiemeijer


    I think it’s because we set the $do_not_duplicate value inside if( is_home() && !is_paged() ) : . On subsequent pages that value isn’t set.

    Hmm…. I can’t think of a better way to show either a sticky, or the latest post if no sticky exists plus 9 more posts on the front page, avoiding duplicates, and then reverting to the default WP settings post count for the remianing pages, while avoiding duplicates between that first post box, and the rest of the stuff

    Moderator keesiemeijer


    Try it with this [not tested]:
    (alteration of your last pastebin)

    Same results…. 1+7, and and 8

    sigh, this is killin me! I thought I had a handle on queries!!
    If anyone is interested, here’s the same topic on SE… no luck there either so far.

    Moderator keesiemeijer


    Maybe a stupid question but, do you have enough posts that aren’t sticky (9)? I tested the last pastebin and it gives me 1 + 9 and 9.
    Maybe there are other loops interfering.

    it does not seem to be caused by the code (i tried in wp3.3beta with a direct copy of as index.php in Twenty Eleven and the test data from and there was 10 posts as expected on each page)
    (only message on page2 as expected:

    Notice: Undefined variable: do_not_duplicate in C:\Xammp\xampp\htdocs\wp-project\wp-content\themes\twentyeleven\index.php on line 42


    what exactly is in do_action( 'voodoo_pre_loop' ); ?

    I have the same theme test unit on my install.

    voodoo_pre_loop does nothing at this time. It’s an empty hook.
    I thought I had it over on SE, but then sadly, nope. I get the right # of posts with the SE code. But it doesn’t use the first loop if no stickies are present. I need the first loop to grab the latest sticky OR latest post, and offload to content-featured

    Also this theme has no other loops. This index.php is all I have (along with the content-$format files which, at this time simply call the title/excerpt for testing.

    The plot thickens here.

    First up, the paginated pages are fine. I made a boneheaded mistake and only output titles for testing. Well, 1 post in the tester has no title. Duh.

    So the paginated pages are good

    However, the home page still is a no go. On further testing, I get 1+7 posts on the home page if a sticky is set. I only get 1+6 if no sticky is set…..

    (do trash posts, drafts, or scheduled posts ave the ability to throw this? 1 of each comes with the test unit…)

    Nailed it over on SE. is the paste (it’s the raw loops, I need to put my stuff in there)

    Readig through it, I can’t tell what caused my loops to poop, and this one to work. Any ideas what I was getting wrong? I’d love to learn from this

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘2 loops not behaving. Count is off’ is closed to new replies.