Support » Fixing WordPress » 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]

    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)
  • 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
    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;

    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.

    Without seeing the code I’m not entirely sure where the problem lies. If you want to throw it in a pastebin 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():

    *There could be typos and bugs in it; I didn’t actually test it. 🙂

    Here is what I have:

    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.