Support » Fixing WordPress » Reset query_posts?

  • Is there a way of ‘resetting’ query_posts? I am using it within the sidebar to display 5 asides, but I still want the loop on the home page to display the number specified in the WordPress options.

    How could I cancel the first query_posts?

    Many Thanks in Advance,

Viewing 3 replies - 1 through 3 (of 3 total)
  • Have you read through the codex docs on The_Loop#Multiple_Loops that should help you.

    No they don’t.. but I will have to manage and find a way around I suppose

    The answer is there in that page.

    You basically have a few different options on how to implement this depending on the order in which you want to run the multiple loops:

    1. Main post loop first followed by asides loop
    2. Asides loop first followed by main post loop

    For 1, you can do the following after running the normal loop where you want you asides loop you call query_posts to get the list of aside posting using the following code:

    // Get the last 5 posts in the asides_cat category.
    <?php query_posts('category_name=asides_cat&showposts=5'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <!-- Do asides stuff... -->
    <?php endwhile;?>

    For 2, which I think is what you want from your description you need to be a little more careful. If you call query_posts for the asides loop before displaying the normal loop it will overwrite the normal loops query and you won’t be able to display it easily. Therefore you have to create a new query object and use that as follows:

    <?php $my_query = new WP_Query('category_name=asides_cat&showposts=5'); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <!-- Do asides_cat stuff... -->
    <?php endwhile; ?>

    You can then run your normal loop after this one for the asides.

    Both these examples come from the codex docs The_Loop#Multiple_Loops_Example_1.

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