[resolved] 2 loops not behaving. Count is off (20 posts)

  1. Rev. Voodoo
    Posted 4 years ago #


    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)

  2. Michael
    Forum Moderator
    Posted 4 years ago #

    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.)

  3. keesiemeijer
    Posted 4 years ago #

    Try if this works [untested]: http://pastebin.com/FbWZEVSh

  4. Rev. Voodoo
    Posted 4 years ago #


    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

  5. Rev. Voodoo
    Posted 4 years ago #

    @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

  6. keesiemeijer
    Posted 4 years ago #

    And this?: [also untested]: http://pastebin.com/YQVkzpZv
    (A little alteration of the first pastebin)

  7. Rev. Voodoo
    Posted 4 years ago #

    @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

  8. keesiemeijer
    Posted 4 years ago #

    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.

  9. Rev. Voodoo
    Posted 4 years ago #

    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

  10. keesiemeijer
    Posted 4 years ago #

    Try it with this [not tested]: http://pastebin.com/ySuaZn5E
    (alteration of your last pastebin)

  11. Rev. Voodoo
    Posted 4 years ago #

    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.

  12. keesiemeijer
    Posted 4 years ago #

    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.

  13. Michael
    Forum Moderator
    Posted 4 years ago #

    it does not seem to be caused by the code (i tried in wp3.3beta with a direct copy of http://pastebin.com/sjPCNNzT as index.php in Twenty Eleven and the test data from https://wpcom-themes.svn.automattic.com/demo/test-data.2011-01-17.xml 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' ); ?

  14. Rev. Voodoo
    Posted 4 years ago #

    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.

  15. Rev. Voodoo
    Posted 4 years ago #

    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...)

  16. Rev. Voodoo
    Posted 4 years ago #

    Nailed it over on SE.

    http://pastebin.com/mrBKYBrU 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

  17. Michael
    Forum Moderator
    Posted 4 years ago #

    possibly the way you dealt with the 'duplicate' caused you to lose a post
    (if( $post->ID == $do_not_duplicate ) continue; just jumps the post out of the given number of posts)

    integrating the $do_not_duplicate into the 'post__not_in' of the query should have worked:

    <?php $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
    $more_args = array(
    	'posts_per_page' => 9,
    	'post__not_in' => array_merge(get_option( 'sticky_posts' ),array($do_not_duplicate)),
    	'paged' => $paged
    query_posts( $more_args ); ?>

    (and remove this: if( $post->ID == $do_not_duplicate ) continue; )

    ref: http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action

    however, as i see, the new code uses 'offset' to jump the first post to a similar effect.

  18. keesiemeijer
    Posted 4 years ago #

    I think it's because you got the query_posts( $args_main_query ); and $args_main_query array out of the conditional if( is_home() && !is_paged() ) that it's able to work right now.

  19. Rev. Voodoo
    Posted 4 years ago #

    @alchymyth, yeah, changing to your suggestion bought me 1 more post.

    My old code still loses 2 on the home page. Either way, I'm coolio, learned a bit today, thanks for the efforts! (you too @keesiemeijer!)

  20. Rev. Voodoo
    Posted 4 years ago #

    OK, the new code works, and the one we worked through here works. It was 1 MISSPELLING on my end for a content-$format file...wow....

Topic Closed

This topic has been closed to new replies.

About this Topic