[wp-hackers] Loop Within A Loop

Eric Mann eric at eamann.com
Tue Jul 20 19:56:16 UTC 2010


I think I know what you're trying to do, but I've never called it a loop within
a loop before.
 
Basically, you need to store the "main" loop's query when you run the second
loop.  Then restore it before closing the loop and things should work out well.
 
There are some pretty good examples in the
Codex: http://codex.wordpress.org/The_Loop#Multiple_Loops_Example_2
 

On July 20, 2010 at 7:46 PM Devin Dixon <devin at tayloegray.com> wrote:

> Hey everyone,
> I am having a problem where I need to call a loop within a loop.The
> code looks somethign like this
>
>  if ( have_posts() ) while ( have_posts() ) : the_post();
>
>        while ( $loop->have_posts() ) : $loop->the_post();
>
>            $wp_query= new WP_Query( array( 'post_type' =>
> "$post_type", 'posts_per_page' => $post_per_page , 'post_parent' =>
>                 $parent_post , 'author' => "$author" ) );
>
>  while ($wp_query->have_posts()) : $wp_query->the_post();
>
> ?>
>
> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent
> Link to <?php the_title_attribute(); ?>"><?php        the_title();
> ?></a>
>
> <?php
>
>        endwhile;
>
>         endwhile;
>
> endif;
>
> The problem is, the class WP_QUERY automatically makes everything
> global. Is there a way where I can call the WP_QUERY object and
> retrieve it values without it affecting the other loop?
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers


More information about the wp-hackers mailing list