WordPress.org

Support

Support » How-To and Troubleshooting » Query within Query?

Query within Query?

  • At the top of my site, just before the main loop, I have a query that pulls the posts from a certain category. Now I got that to work fine but I wanted to step it up a notch…

    The section is designed so the big main div shows the latest post from a category, while inside that box a small section shows the other posts by date from the same category, using an offset condition.

    However that’s how I see it on paper, how do I integrate the two queries so one can work within the other.

    Basically it would be:

    <?php $my_query = new WP_Query(‘category_name=featured&showposts=1’); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    [HTML showing one post]

    ====== sub query =======
    <?php $my_query = new WP_Query(‘category_name=featured&showposts=3&offset=1’); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    [HTML sub stuff with 1 post offset]
    <?php endwhile; ?>
    ====== sub query =======

    <?php endwhile; ?>

    Obviously wrapping a query within a query like what I did above doenst make sense… so how do I do it?

Viewing 1 replies (of 1 total)
  • ====== sub query =======

    <?php $my_query2 = new WP_Query('category_name=featured&showposts=3&offset=1'); ?>
    <?php while ($my_query2->have_posts()) : $my_query2->the_post(); ?>
    [HTML sub stuff with 1 post offset]
    <?php endwhile; ?>

    ====== sub query =======

Viewing 1 replies (of 1 total)
  • The topic ‘Query within Query?’ is closed to new replies.