Title: multiple loops question
Last modified: August 20, 2016

---

# multiple loops question

 *  [wplearner](https://wordpress.org/support/users/wordpresslearner/)
 * (@wordpresslearner)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/multiple-loops-question-2-2/)
 * hey, I have a question about multiple loops in a template, and basically just
   want to make sure I am doing it right. say I have a custom template and for whatever
   reason I want a first loop that displays two posts:
 *     ```
       <?php
       $myPosts = new WP_Query('posts_per_page=2''cat=2,6');
   
       while($myPosts->have_posts()) : $myPosts->the_post();
       ?><?php the_content();?>
       <?php endwhile; ?>
   
       //and then for whatever reason another loop(even if could be done in
       // the first one..
   
       <?php
       $myPosts = new WP_Query('posts_per_page=1''cat=3');
   
       while($myPosts->have_posts()) : $myPosts->the_post();
       ?><?php the_content();?>
       <?php endwhile; ?>
       ```
   
 * would that be an okay way of doing this, or is there a better way to have two
   loops in a template?

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

 *  [Big Bagel](https://wordpress.org/support/users/big-bagel/)
 * (@big-bagel)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/multiple-loops-question-2-2/#post-2394918)
 * That’s basically it for creating two new loops. A few things though.
 * The arguments for your queries need to be either a single string or an array.
   For example:
 *     ```
       $myPosts = new WP_Query( 'posts_per_page=2&cat=2,6' );
       ```
   
 * or
 *     ```
       $args = array(
           'posts_per_page' => 2,
           'category__in' => array( 2, 6 )
       );
   
       $myPosts = new WP_Query( $args );
       ```
   
 * I prefer using arrays over query strings as it’s more readable, and `category_in`
   over `cat`. 🙂
    You can see more examples here:
 * [Class Reference/WP Query](http://codex.wordpress.org/Class_Reference/WP_Query)
 * It’s not necessary, but since you’re reusing the `$myPosts` variable anyways,
   it might be more efficient to also reuse the WP_Query object too:
 *     ```
       $myPosts = new WP_Query( 'posts_per_page=2&cat=2,6' );
   
       /* Everything in-between */
   
       $myPosts = $myPosts->query( 'posts_per_page=1&cat=3' );
       ```
   
 * Of course, don’t forget to add any extra stuff you want to your actual loops.
   Right now it would only show the actual content of each post.
 *  Thread Starter [wplearner](https://wordpress.org/support/users/wordpresslearner/)
 * (@wordpresslearner)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/multiple-loops-question-2-2/#post-2394927)
 * hey thanks for the reply, so you’re suggesting using
 * `$myPosts = $myPosts->query( 'posts_per_page=1&cat=3' );`
 * instead of the second while loop?
 *  [Big Bagel](https://wordpress.org/support/users/big-bagel/)
 * (@big-bagel)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/multiple-loops-question-2-2/#post-2394928)
 * Your loops are fine. It’s the parameter strings for your queries that are a bit
   off:
 * Your first query:
 *     ```
       $myPosts = new WP_Query( 'posts_per_page=2''cat=2,6' );
       ```
   
 * should be:
 *     ```
       $myPosts = new WP_Query( 'posts_per_page=2&cat=2,6' );
       ```
   
 * and your second query:
 *     ```
       $myPosts = new WP_Query( 'posts_per_page=1''cat=3' );
       ```
   
 * should be:
 *     ```
       $myPosts = new WP_Query( 'posts_per_page=1&cat=3' );
       ```
   
 * [Class Reference/WP Query](http://codex.wordpress.org/Class_Reference/WP_Query)
 *  Thread Starter [wplearner](https://wordpress.org/support/users/wordpresslearner/)
 * (@wordpresslearner)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/multiple-loops-question-2-2/#post-2394941)
 * yeah I saw that part.. I guess the main reason I was asking is because I read
   something about having multiple queries can be heavy on the server, so I was 
   wondering if there were a better way of calling two loops..
 * I understand there are a couple other methods, (query_posts was one maybe), but
   I read that it has some downsides, so I just wanted to see if calling two loops
   seperately was ok, or if one should be nested, or some such.
 *  [Big Bagel](https://wordpress.org/support/users/big-bagel/)
 * (@big-bagel)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/multiple-loops-question-2-2/#post-2394945)
 * `query_posts()` is for altering the main loop and is thus less ideal when the
   goal to create additional loops; `get_posts()` is just a wrapper around the creation
   of a new WP_Query object. So, while adding extra queries can be expensive depending
   on what’s being queried, if you want additional loops, using WP_Query is the 
   way to go. Having two queries and two loops is perfectly fine if that’s what 
   you need to do.
 *  [mishamean](https://wordpress.org/support/users/mishamean/)
 * (@mishamean)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/multiple-loops-question-2-2/#post-2394974)
 * I have a little problem with multiple loops also. I’m trying to do side by side
   posts, but on a page only. Something like an pro/con debate. Here is my code:
 * _[code moderated - please follow the [forum guidelines for posting code](http://codex.wordpress.org/Forum_Welcome#Posting_Code)]_
 * This code shows me the column allright but on the first page I have only category
   15 post, the second one only the 16th and so on... Anybody any ideas about how
   can I make 15th post and 16th post appear on one page?
    Thank you
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/multiple-loops-question-2-2/#post-2394979)
 * [@mishamean](https://wordpress.org/support/users/mishamean/)
    please start your
   own topic
 * please possibly post a link to your site, and use the [http://pastebin.com/](http://pastebin.com/)
   for your code – [http://codex.wordpress.org/Forum_Welcome#Posting_Code](http://codex.wordpress.org/Forum_Welcome#Posting_Code)
 *  Thread Starter [wplearner](https://wordpress.org/support/users/wordpresslearner/)
 * (@wordpresslearner)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/multiple-loops-question-2-2/#post-2395029)
 * hey bagel, thanks man appreciate it.

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

The topic ‘multiple loops question’ is closed to new replies.

 * 8 replies
 * 4 participants
 * Last reply from: [wplearner](https://wordpress.org/support/users/wordpresslearner/)
 * Last activity: [14 years, 7 months ago](https://wordpress.org/support/topic/multiple-loops-question-2-2/#post-2395029)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
