WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working on a project where I need display 2 post from one category and also I need to put another loop between this post post to display random list of post from other category. Easiest way to do so is to create 3 queries, but when I created 2 queries for one category, but here I got problem one loop breaks and display both posts, second works fine. I used offset parameter for second query but it doesn't work.


        $first_query_args = array(
          'category_name' => 'first-category',
          'tag' => 'special-tag',
          'post_per_page' => 1,

        $first_query = new WP_Query( $first_query_args );

        if ( $first_query->have_posts() ) : while ( $first_query->have_posts() ) : $first_query->the_post();

        // First query stuff goes here

        endwhile; endif;


        // Query for Random posts
        $query_for_random_args = array(
            'category_name' => 'cat-for-rand',
            'post_per_page' => 3,
            'orderby' => 'rand',

        $query_for_random = new WP_Query( $query_for_random_args );

        if ( $query_for_random->have_posts() ) :


        <?php while ( $query_for_random->have_posts() ) : $query_for_random->the_post(); ?>

                <?php if ( has_post_thumbnail() ) { ?>
                    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'list-image' ); ?></a>
                    <a href="<?php the_permalink(); ?>" ><?php the_title(); ?></a>

        <?php } endwhile; ?>


        <?php endif; wp_reset_query(); ?>


        $second_query_args = array(
          'category_name' => 'first-category',
          'tag' => 'special-tag',
          'offset' => 1,
          'post_per_page' => 1,

        $second_query = new WP_Query( $second_query_args );

        if ( $second->have_posts() ) : while ( $second_query->have_posts() ) : $second_query->the_post();

        // Second query stuff goes here

        endwhile; endif;

( This code What I use for those loops )

Any ideas how can I solve this problem? or how can I use 2 queries for similar task?



share|improve this question
Show the code you tried = best. – kaiser May 18 '11 at 16:15
up vote 1 down vote accepted

I would use get_posts() for your two ad hoc Loops. (Actually, you only need one ad hoc Loop, if you're modifying the main Loop so that only 2 posts are retrieved.) e.g.

\\ code modify main Loop to return only 2 posts goes here

$firstpost = true;

if ( have_posts() ) : while ( have_posts() ) : the_post();

    \\ main Loop Post output goes here

    if ( $firstpost ) {

        $randomposts = get_posts( array( 'category' => $categoryid, 'numberposts' = $numberposts, 'orderby' => 'rand' );

        \\ Output ad-hod Loop content, e.g.
        foreach ( $randomposts as $randompost ) { ?>
            <li><a href="<?php echo get_permalink( $randompost->ID ); ?>"><?php echo $randompost->post_title; ?></a></li>
        <?php } ?>
    <?php }

   $firstpost = false;

// end the main Loop
endwhile; endif;

(Untested example code)

share|improve this answer
Hello Chip. Thanks for help. Yes I know that I can modify main loop with query_post. I thins situation I'm more interested if I can inset loop for random post like people use to insert google ads after first post or between posts – Mamaduka May 18 '11 at 21:07
@Mamaduka yes, you can. That's what the $randomposts = get_posts() business is. – Chip Bennett May 18 '11 at 23:20
@Chip Bennett I tested your code example works great, but it outputs random post after each post from main loop. It great if you want place content after each post, in this example I only want to display random post after fist post – Mamaduka May 19 '11 at 9:12
@Mamaduka I may have misread your OP. I assumed there would only be two posts in the main Loop. You could always add a variable, and only output the ad hoc Loop if the variable isn't set. Let me update my answer to give an example. – Chip Bennett May 19 '11 at 12:39
(Answer updated per above comment.) – Chip Bennett May 19 '11 at 12:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.