• Resolved crazydrumguy

    (@crazydrumguy)


    I am using query_posts() on my site to display the most recent post in a box on the side of the page. Is there any way I can use query_posts() a second time to display something else in the main content area? (See crazydrumguy.info for an example.) The message on the green background is static, but I’d like to have it pull from another category in the blog. Can I use query_posts() twice in the same Loop? Can I create two Loops on one page?

Viewing 5 replies - 1 through 5 (of 5 total)
  • You cannot use query_posts() more than once on a particular post loop, but you can set up (and initialize with query_posts()) as many loops as you like.

    Thread Starter crazydrumguy

    (@crazydrumguy)

    Thanks! I currently have the call to query_posts() at the very top of my page (immediately under get_header()). Is there a certain place where I should place it for it to work with both loops? Can it go within the loop itself?

    Can it go within the loop itself?

    No. It needs to go before it.

    Each time you need a new loop you have to set up another one, meaning another loop, in the location you want it. A very basic (and non-working) example would be:

    <?php query_posts('FIRST QUERY ARGS'); ?>
    <?php while(have_posts()) : the_post(); ?>
    <?php the_title(); ?>
    <?php the_content(); ?>
    <?php endwhile; ?>

    <?php query_posts('SECOND QUERY ARGS'); ?>
    <?php while(have_posts()) : the_post(); ?>
    <?php the_title(); ?>
    <?php the_content(); ?>
    <?php endwhile; ?>

    And so on for each loop.

    Thread Starter crazydrumguy

    (@crazydrumguy)

    Thanks for your help. The query_posts() codex entry really needs some work…. ??

    I prefer to just do it manually:

    $posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE [stuff here]");

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘query_posts() twice?’ is closed to new replies.