WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] query posts BEFORE the main loop (8 posts)

  1. lobsterman
    Member
    Posted 8 years ago #

    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?

  2. Kafkaesqui

    Posted 8 years ago #

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

  3. lobsterman
    Member
    Posted 8 years ago #

    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?

  4. Kafkaesqui

    Posted 8 years ago #

    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

  5. lobsterman
    Member
    Posted 8 years ago #

    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?

  6. SavoryMedia
    Member
    Posted 8 years ago #

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

  7. Kafkaesqui

    Posted 8 years ago #

    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.

  8. lobsterman
    Member
    Posted 8 years ago #

    Thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.