0

I have a query that is pulling posts from all categories except one with an ID of 2. Within this first loop I have an if statement to count even and odd posts to alter my html(which is working fine).

At the end of every 2 posts a div is inserted (which I can do), and within that div I need to query a random post from the category with the ID of 2.

Having trouble figuring out how to loop within a loop. I've seen a few different examples but none are seeming to work.

Here is my code slimmed down a bit.

<?php 
$counter = 0;
query_posts('cat=-2&orderby=date&order=DESC'); while (have_posts()): the_post(); $counter++; ?>
    <?php if( $counter % 2 == 0 ) : ?>

        <!-- Displaying even posts here -->

    <?php else: ?>

        <!-- Displaying odd posts here -->

    <?php endif; ?>
    <?php if( $wp_query->current_post == 1 ) { ?>
        <div class="post__splitter">

            <!-- This is where a need to query a random post from the category with an ID of 2.  -->
            <!-- query_posts('cat=2&orderby=rand&post_per_page=-1') -->

        </div>
    <?php } ?>
<?php endwhile; ?>

Edit:

Here's my full code so far... running into issues where it's displaying all posts, plus like 10 million of them.. Can't figure it out...

<?php 
$counter = 0;
$outer_query = new WP_Query( array( 'cat' => '-2', 'orderby' => 'date', 'order' => 'DESC' ) ); while ($outer_query->have_posts()): the_post(); ?>
    <?php if( $counter % 2 == 0 ) : ?>  
        <div class="post">
            <div class="post__thumbnail"><?php the_post_thumbnail(); ?></div>
            <div class="post__content_wrapper">
                <div class="post__content">
                    <div class="post__title"><a href="<?php the_permalink();?>"><?php the_title(); ?></a></div>
                    <div class="post__caption"><?php the_excerpt(); ?></div>
                    <div class="post__details">
                        <a class="post__date" href="<?php the_permalink();?>"><?php the_date('F j, Y', 'Posted on '); ?></a>
                        <?php the_tags('<span class="post__tags">Tagged with: ',', ','</span>'); ?>
                    </div>
                </div>
            </div>
        </div>
    <?php else: ?> 
        <div class="post post--odd">
            <div class="post__content_wrapper">
                <div class="post__content">
                    <div class="post__title"><a href="<?php the_permalink();?>"><?php the_title(); ?></a></div>
                    <div class="post__caption"><?php the_excerpt(); ?></div>
                    <div class="post__details">
                        <a class="post__date" href="<?php the_permalink();?>"><?php the_date('F j, Y', 'Posted on '); ?></a>
                        <?php the_tags('<span class="post__tags">Tagged with: ',', ','</span>'); ?>
                    </div>
                </div>
            </div>
            <div class="post__thumbnail"><?php the_post_thumbnail(); ?></div>
        </div>
    <?php endif; ?>
    <?php if( $wp_query->current_post == 1 ) { ?>
        <div class="post__splitter">
            <?php
                $inner_query = new WP_Query(array('cat' => 2, 'orderby' => 'rand'));    

                the_content();
            ?>
        </div>
    <?php } ?>
<?php endwhile; ?>

Final Working Edit

<?php 
$counter = 0;
$args = array (
    'order' => 'DESC',
    'orderby'=> 'date',
    'cat' => '-2',
);

// The Query
$outer_query = new WP_Query( $args ); if ( $outer_query->have_posts() ) : while ( $outer_query->have_posts() ) : $outer_query->the_post(); $counter++; ?>
<?php if( $counter % 2 == 0 ) : ?>  
        // even posts
    <?php else: ?> 
        // odd posts
    <?php endif; ?>
    <?php if( $outer_query->current_post ==1 ) { ?>

        <?php $args2 = array(
            'cat' => '2',
            'posts_per_page' => '1',
            'orderby' => 'rand',
        );
        $inner_query = new WP_Query( $args2 ); if ( $inner_query->have_posts() ) : while ( $inner_query->have_posts() ) : $inner_query->the_post(); ?>

            // inner query pulling random post. 

        <?php endwhile; endif; ?>
    <?php } ?>

<?php endwhile; endif; ?>
0

You should not be using query_posts() for this or anything else.

For secondary loops you should be making new WP_Query instances. Since you can (and should) assign instances to different variables they won't interfere with each other.

So your structure would be roughly like this:

$outer_query = new WP_Query();

while( $outer_query->have_posts() ) : the_post;

    $inner_query = new WP_Query();

endwhile;
  • I'm having an issue getting this to work with the parameters i'm setting for each query... mind editing to help me out? – jobiin Oct 26 '16 at 18:57
  • Please update your question with your progress and what is your current roadblock. :) – Rarst Oct 26 '16 at 19:04
  • added my current code. – jobiin Oct 26 '16 at 19:13
  • You are still referencing global $wp_query and not actually running loop properly of your instance. I would recommend to read up on WP_Query documentation a bit. Don't try everything at once, get simple outer case working, then add more logic to that. – Rarst Oct 26 '16 at 19:19
  • I've posted my final code. Got it working. Just had to start over instead of trying to edit. I'm accepting your answer as correct since it pointed me in the correct direction. – jobiin Oct 28 '16 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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