How to do a loop inside a loop?

I'm trying to get posts from a custom posts using WP_Query.

ul
    ?php $query1 = query_posts( array( 'posts_per_page' = -1, 'post_type' = array('specialties') ));
        if(have_posts()): while(have_posts()): the_post();
    ?
    li
        !-- title from post type specialties --
        p?php the_title(); ?/p
    /li
    ?php  
        endwhile; endif; wp_reset_query();
    ?
/ul

It's working just fine. Now I need to get posts from another custom post and show inside the already created loop. Now the code looks like this:

ul
    ?php $query1 = query_posts( array( 'posts_per_page' = -1, 'post_type' = array('specialties') ));
        if(have_posts()): while(have_posts()): the_post();
    ?
    li
        !-- title from post type specialties --
        p?php the_title(); ?/p
        ul
            ?php query_posts( array( 'posts_per_page' = -1, 'post_type' = array('team') ));
                if(have_posts()): while(have_posts()): the_post();
            ?
            !-- title from post type team --
            lia href="" class=""?php the_title() ?/a/li
            ?php  
                endwhile; endif;
            ?
        /ul
    /li
    ?php  
        endwhile; endif; wp_reset_query();
    ?
/ul

As you can see, I'm trying to create a loop (inside another loop) to show posts from another custom post, but its not working. I also tried to add wp_reset_query() after the new endif; but it didn't work neither.

What am I doing wrong? I can't figure out what the problem is. Some help would be appreciated.

Topic wp-reset-query loop wp-query Wordpress

Category Web


What if you save each query in a variable ?

<ul>
    <?php $query1 = query_posts( array( 'posts_per_page' => -1, 'post_type' => array('specialties') ));
        if($query1->have_posts()): while($query1->have_posts()): $query1->the_post();
    ?>
    <li>
        <!-- title from post type specialties -->
        <p><?php the_title(); ?></p>
        <ul>
            <?php $query2 = query_posts( array( 'posts_per_page' => -1, 'post_type' => array('team') ));
                if($query2->have_posts()): while($query2->have_posts()): $query2->the_post();
            ?>
            <!-- title from post type team -->
            <li><a href="" class=""><?php the_title() ?></a></li>
            <?php  
                endwhile; endif;
            ?>
        </ul>
    </li>
    <?php  
        endwhile; endif; wp_reset_query();
    ?>
</ul>

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.