How do I Use Multiple Loops with WP_Query?

Hi I am trying to add post from a certain taxonomy term to a cpt loop. The code below is the code I am using.

    <?php // Create empty array to store post ids in
 $excludes = array();

                 $args=array(
                 'post_type' => array ('gallery','videos'),
     'taxonomy'=>'series',
     'term' => 'pretty-little-liars',
                 'post_status' => 'publish',
                );
               $my_query = new WP_Query( $args );
if ($my_query->have_posts()) : while($my_query->have_posts()) : $my_query->the_post(); ?>

But the problem with the code is that all of my post are now missing. I then learned that I needed to add a second loop. Ok so after reading up on WP_Query

Read More

I tried to add a second loop but it gave me an error, and here is the code i used for that

        <?php // Create empty array to store post ids in
 $excludes = array();

                 $args=array(
                 'post_type' => array ('gallery','videos'),
     'taxonomy'=>'series',
     'term' => 'pretty-little-liars',
                 'post_status' => 'publish',
                );
               $my_query = new WP_Query( $args );
if ($my_query->have_posts()) : while($my_query->have_posts()) : $my_query->the_post(); 

// Restore original Query & Post Data
wp_reset_query();
wp_reset_postdata();

/* The 2nd Query (without global var) */
$query2 = new WP_Query( $args2 );

// The 2nd Loop
if ($query2->have_posts()) : while($query2->have_posts()) : $query2->the_post();

// Restore original Query & Post Data
wp_reset_query();
wp_reset_postdata();

?>

It gave me this error

Parse error: syntax error, unexpected $end in /hermes/bosweb/web188/b1885/ipg.celebloidcom1/tvcafe/wp-content/themes/tvcafe/archive-pretty-little-liars.php on line 258

What am I doing wrong here, can anyone help me fix this. I am not going for a seperated structure in my loop but more of a together structure.

For Example I don’t want the first loop at the top and the second loop at the bottom. I want them combined in one loop.

If you need more info or would like to see my whole php please let me know.

Related posts

Leave a Reply

2 comments

  1. As Toscho mentionned above, you didn’t close your if and while. When in doubt, indent your codes to avoid confusion.

    $my_query = new WP_Query( $args );
    if ($my_query->have_posts()) :
      while($my_query->have_posts()) :
        $my_query->the_post();
      endwhile;
    endif;
    wp_reset_postdata();
    
    /* The 2nd Query (without global var) */
    $query2 = new WP_Query( $args2 );
    
    // The 2nd Loop
    if ($query2->have_posts()) :
      while($query2->have_posts()) :
        $query2->the_post();
      endwhile;
    endif;
    wp_reset_postdata();
    

    Also, you don’t need to use wp_reset_query() with WP_Query. This question deals with that issue.

  2. Looking at your file you never declare $args2, also your page is really not written correctly. You are only getting one post because you are not in the loop properly, it is assuming it is inside a loop but it isn’t.