Title: Multiple Nested Loops?
Last modified: August 20, 2016

---

# Multiple Nested Loops?

 *  [NikkiAddams](https://wordpress.org/support/users/nikkiaddams/)
 * (@nikkiaddams)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/multiple-nested-loops/)
 * I have been struggling for days trying to get this to work, but I can’t get it
   right. I am trying create a spot to show a random featured artist every time 
   the page is loaded.
 * In this spot, I created a loop to pull a random post within the “artist” category,
   along with the thumbnail, name and custom fields. I got this to work just like
   I wanted.
 * However, I also want to add a row of video thumbnails from the videos category,
   using the current loop to get the name from the video tags. Below that, I want
   a row of photos from the photos category, which also have the artist name in 
   the tag.
 * I can get each function to work as it should…but I can’t get them to all work
   together at once. The first and second loop work, but the third is getting it’s
   info from the second loop instead of the first, like the second loop is still
   open. I’m still learning WP, so my guess is I’m using loops and queries the wrong
   way 🙁
 * This is essentially what I’m trying to do:
    [first query to get random artist]
   Featured Artist: {artist thumb} {Artist Name (post title)} {custom field in post}
 * Videos featuring {Artist Name}
    [second query to get videos] {video post thumb}{
   video post thumb} {video post thumb} [end video query]
 * Photos featuring {Artist Name}
    [third query to get photos] {photo post thumb}{
   photo post thumb} {photo post thumb} [end photo query] [end artist query]
 * This is the mess I have now. It keeps re-looping and the 3rd photo loop is reading
   from the 2nd loop:
 * _[code moderated - please follow the [forum guidelines for posting code](http://codex.wordpress.org/Forum_Welcome#Posting_Code)]_
 * Any help would be greatly appreciated. I'm starting to pullout my hair figuring
   out WP loops!
    ~ Nicole

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [Big Bagel](https://wordpress.org/support/users/big-bagel/)
 * (@big-bagel)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/multiple-nested-loops/#post-2377618)
 * Instead of `query_args()`, which alters the main loop, it would be better to 
   create secondary loops with `WP_Query()` or `get_posts()` so they don’t conflict
   with each other.
 * [Class Reference/WP Query](http://codex.wordpress.org/Class_Reference/WP_Query)
   
   [Template Tags/get posts](http://codex.wordpress.org/Template_Tags/get_posts)
 * Using part of your code above, for example:
 *     ```
       $cats = get_categories( 'include=3' );
       foreach ($cats as $feature) :
           $first_query_args = array(
               'posts_per_page' => 1,
               'orderby' => rand,
               'cat' => $feature->term_id
           );
   
           $first_query = new WP_Query( $first_query_args );
           if ( $first_query->have_posts() ) : while ( $first_query->have_posts() ) : $first_query->the_post();
               /* And so on... */
   
           endwhile; endif;
       endforeach;
       ```
   
 *  Thread Starter [NikkiAddams](https://wordpress.org/support/users/nikkiaddams/)
 * (@nikkiaddams)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/multiple-nested-loops/#post-2377657)
 * Thank you! 🙂 I think that’s what I did previously, but I had one issue I couldn’t
   figure out. When using secondary loops with WP_Query(), this happens:
 * Between the 2nd and 3rd loops is one line of text that needs to display the_title_attribute()
   from the 1st loop. The title should be “Artist Name”. However, the title is now
   being displayed as the title of the last post displayed in the 2nd (video) loop.
   In other words, the_title_attribute() shows “Photos of Video Post #3:” instead
   of “Photos of Artist Name:”.
 * Am I not ending the 2nd loop properly? I am using endwhile; endif; endforeach;.
   If it weren’t for that one line, everything works perfectly using the WP_Query()
   loops example you gave.
 *  [Big Bagel](https://wordpress.org/support/users/big-bagel/)
 * (@big-bagel)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/multiple-nested-loops/#post-2377692)
 * Without seeing the code I’m not entirely sure where the problem lies. If you 
   want to throw it in a [pastebin](http://pastebin.com/) I’d be happy to have a
   look at it. Here’s an example* of how I would convert your code in the first 
   post to use `WP_Query()`:
 * [http://pastebin.com/0ZkALcph](http://pastebin.com/0ZkALcph)
 * *There could be typos and bugs in it; I didn’t actually test it. 🙂
 *  Thread Starter [NikkiAddams](https://wordpress.org/support/users/nikkiaddams/)
 * (@nikkiaddams)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/multiple-nested-loops/#post-2377759)
 * Here is what I have:
 * [http://pastebin.com/8e7qUHsA](http://pastebin.com/8e7qUHsA)
 *  [Big Bagel](https://wordpress.org/support/users/big-bagel/)
 * (@big-bagel)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/multiple-nested-loops/#post-2377782)
 * After re-reading what the problem was, I didn’t actually need to see the code.
   Whoops. 🙂
 * When you call `the_post()` inside any of the loops, it sets up that post’s data
   into global variables which aren’t reset once the loop is done. So, after your
   second loop is done running, the global post data is still set to the last post.
   There are a couple things you can do, but I think the easiest would be to set
   a variable up above the second loop with the data you want, then echo it later:
 * `$main_title_attr = the_title_attribute( '', '', 0 );`

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Multiple Nested Loops?’ is closed to new replies.

## Tags

 * [loops](https://wordpress.org/support/topic-tag/loops/)
 * [multiple](https://wordpress.org/support/topic-tag/multiple/)
 * [nested](https://wordpress.org/support/topic-tag/nested/)
 * [queries](https://wordpress.org/support/topic-tag/queries/)
 * [query](https://wordpress.org/support/topic-tag/query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 2 participants
 * Last reply from: [Big Bagel](https://wordpress.org/support/users/big-bagel/)
 * Last activity: [14 years, 7 months ago](https://wordpress.org/support/topic/multiple-nested-loops/#post-2377782)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
