WordPress.org

Support

Support » Requests and Feedback » [Resolved] query posts BEFORE the main loop

[Resolved] query posts BEFORE the main loop

  • I want to display a custom post on top of each page, the problem is, it will reset the loop, so the main loop won’t show the right thing for that page. is there any way around it except putting the code itself under the main loop?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Start of your custom post loop:

    <?php
    $custom = new WP_Query('your_args');
    while($custom->have_posts()) : $custom->the_post();
    ?>

    When setting up a new query class through WP_Query(), note it accepts all the same arguments as query_posts().

    Thanks allot, that’s exactly what I needed.
    Just one more thing, I remember reading somewhere how to do this, but I cant remember where. how do I offset the posts to start from the 3rd post?

    You can’t. At least, not with query_posts() or by setting up a new WP_Query class. For an offset you’ll have to stick with using an older method:

    http://codex.wordpress.org/Template_Tags/get_posts

    This works great, Thanks!
    Just for general reference, what’s the difference between this and query posts? this seems to be better for multiple loops, is that true?

    “You can’t. At least, not with query_posts() or by setting up a new WP_Query class. For an offset you’ll have to stick with using an older method:

    http://codex.wordpress.org/Template_Tags/get_posts

    Actually, I DID use query_posts() with some more elements to get a list with one main post and a list of more posts…with an offset.

    Check the second code example here.

    SavoryMedia, I’ll amend my previous claim and say it can’t be done without some custom coding outside the function. But I’m still technically right. ;)

    lobsterman, each has its limitations. get_posts certainly is beneficial with its ‘offset’ param, whereas query_posts/WP_Query provides a broader mechanism and works off the standard post loop. Sort of a coin toss in many cases, though.

    Thanks.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘[Resolved] query posts BEFORE the main loop’ is closed to new replies.