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
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.
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?
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
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.
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.
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]
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
@mishamean
please start your own topic
please possibly post a link to your site, and use the http://pastebin.com/ for your code – http://codex.wordpress.org/Forum_Welcome#Posting_Code
hey bagel, thanks man appreciate it.