7

I'm using a couple of custom wp_query loops on my page, the first one retrieves news from a certain category then displays a small excerpt from it with the permalink.

The second one is another wp_query that gets a custom post type with a few advanced custom fields.

The problem is I'd like another loop within the second one that gets 3 posts from the news section with a thumbnail (basically the opposite of the first loop, it'll get all the other categories). After reading countless articles on the loop I can't figure out how to make a 'nested' loop within my second loop. I'm sure it's got to be simple, seems like something that'd be easy to do.

Here's my code with a lot of html stripped out.

<?php 
/*
*  Template Name: Homepage
*/
?>

<?php get_header(); ?>

<div class="thenews">

    <div class="newsinner">

        <div class="grid-1">
            <h6 class="nsix">latest news</h6>
        </div> <!-- end div grid-1 -->

        <div class="grid-2">
            <?php
            $recentPosts = new WP_Query();
            $recentPosts->query('cat=5&showposts=1');
            ?>
            <?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
            <p><?php echo ''.get_the_twitter_excerpt(); ?>...</p>
        </div> <!-- end div grid-2 -->   

        <div class="grid-3">
            <a href="<?php the_permalink() ?>">Read it</a>
            <?php endwhile; wp_reset_query(); ?>
        </div> <!-- end div grid-3 -->

    </div> <!-- end div newsinner -->

</div> <!-- end div thenews -->

<div id="main-content">

    <div class="typograhpy">

        <div class="home-grid-1">

            <div class="home-grid-1-inner">
                 <?php
                 $portfolio_query = new WP_Query(array(
                    'post_type' => 'headerhome',
                    'showposts' => 1
                ) );
                ?>
                <?php while ($portfolio_query->have_posts()) : $portfolio_query->the_post(); ?>
                <h2><?php the_title(); ?></h2>  
                <?php the_content(); ?>

                <div class="anyres">
                    <p> <?php the_field('anyresolution'); ?></p>
                </div> <!-- end div anyres -->

            </div> <!-- end home-gird-1-inner -->

        </div> <!-- end home-grid-1 -->

        <div class="home-grid-2">

            <div class="inner-home-1">
                <div class="inside-home-1-1">
                 <h2><?php the_field('services_'); ?></h2>
                 <p> <?php the_field('services_text'); ?></p>
                </div> <!-- end div inside-home-1-1 -->
                <div class="inside-home-1-2">
                    <p> <?php the_field('services_text_right'); ?></p>
                </div> <!-- end div inside-home-1-2 -->
            </div> <!-- end div inner-home-1 -->

            <div class="margindiv"></div>

            <div class="inner-home-2">
                <div class="brushpic"></div> <!-- end div brushpic -->
                <h3><?php the_field('services_1_header') ?></h3>       
                <p><?php the_field('services_1_content'); ?></p>
            </div><!-- end div inner-home-2 -->

            <div class="inner-home-3">
                <div class="cloudpic"></div> <!-- end div cloudpic -->
                <h3><?php the_field('services_2_header') ?></h3>       
                <p><?php the_field('services_2_content'); ?></p>
            </div> <!-- end div inner-home-3 -->

            <div class="inner-home-4">
                <div class="onetwoone"></div> <!-- end div onetwoone -->
                <h3><?php the_field('services_3_header') ?></h3>       
                <p><?php the_field('services_3_content'); ?></p>
            </div> <!-- end div inner-home-4 -->

        </div> <!-- end div home-grid-2 -->

        <div style="clear:both"></div>

    </div> <!-- end div typograhpy -->

    <div class="graphgrid">
    </div> <!-- end div graphgrid -->

    <div class="sizesdes">                
        <blockquote><?php the_field('gallery_blockquote_') ?></blockquote>     
        <p><?php the_field('gallery_content'); ?></p>
        <?php endwhile; wp_reset_query(); ?>
    </div> 

</div><!-- end div main-content -->
<?php get_footer(); ?>

Working code, though it won't retrieve the_post_thumbnail

<?php
global $post;$backup=$post;
$inner_query = new WP_Query();
$inner_query->query('showposts=3');
?>

<?php while ($inner_query->have_posts()) : $inner_query->the_post(); $post=$backup; ?>

    <?php the_post_thumbnail(); ?>

    <p><?php echo ''.get_the_custom_excerpt(); ?>...</p>

    <a href="<?php the_permalink() ?>">Read it</a>

<?php endwhile; ?>
1
  • Do the exact same thing. Set a new variable within the second while loop, and use $that_query->has_posts() and ->the_post(), but make sure you are not using any of the data from the second loop after the third is started. Also make sure to use wp_query_reset() after your while loop. Commented Nov 6, 2012 at 14:17

3 Answers 3

19

You can create any number of loops as you wish by creating more WP_Query objects

$query = new WP_Query($args);

while ($query->have_posts()) :

    // initialization for $inner_args & backup the current global $post
    $inner_query = new WP_Query($inner_args);

    while ($inner_query->have_posts()) :
        // do something
    endwhile;
    // restore the global $post from the previously created backup

endwhile;

EXPLANATION AS REQUESTED

Whenever you call a template tag such as the_title();, it displays something about the current post in the loop. But how does it know which post is the current post? It is done by reading the information from the global postdata (stored in a global variable $post)

When using a loop you always use a $query->the_post() as the first statement. What this function do is set that global data to the next post from the WP_Query object (the previous contents are lost)

Here when you called the inner loop, the postdata related to the outer loop was getting lost when the inner loop starts working. Then whatever function you use after the end of inner loop is still finding only the inner loop's data.

In the solution you first saved the outer loop's data in another variable before the contents get lost. Then the loop works as it's supposed to be(deleting all outer loop data).

Then when the inner loop's work is done, you now need to use the outer loop's data but it is lost due to the inner loop. This is where you take the previously saved data & replace it. Now you're back to the position you were at when you started the inner loop

8
  • I edited my first post, can't get this to work properly. Though I'm sure I'm doing something wrong.
    – andy
    Commented Nov 6, 2012 at 15:50
  • Did you backup the global postdata? Add global $post;$backup=$post; before the inner while & $post=$backup; after the inner while. Commented Nov 6, 2012 at 16:41
  • Ah it works when I write it like this (see edit to first post), but the_post_thumbnail no longer works.
    – andy
    Commented Nov 6, 2012 at 16:47
  • $post=$backup; goes after the while loop i.e. after the endwhile; Commented Nov 6, 2012 at 16:52
  • Ah right, now it works. Can you explain what global $post;$backup=$post; in the context of the loop? Just so I know what I'm actually doing. And thanks for your help, appreciate it!
    – andy
    Commented Nov 6, 2012 at 17:03
10

You can use WP_Query::reset_postdata() for this too.

$query = new WP_Query($args);

while ($query->have_posts()) :

    // initialization for $inner_args & backup the current global $post
    $inner_query = new WP_Query($inner_args);

    while ($inner_query->have_posts()) :
        // do something
    endwhile;

    // restore the global $post from the previously created backup
    $query->reset_postdata();

endwhile;
1

Add global $post;$backup=$post; before the inner while & $post=$backup; after the inner while.

(Why didn't Mridul Aggarwal write the real thing in his code part?)

1
  • Your answer is off to a good start. Please consider explaining how and why this code solves the problem so others can learn from it. Check the guide to answers for further advice. Commented Apr 3, 2021 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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