Own your WordPress loop with wp_query

Image by yaz36
The WordPress loop is arguably the heart of any WP powered site. With all this power at its fingertips, it obviously has some serious customization potential. I’m going to explain a couple of ways here of how you can fully use the potential of your WP CMS’s loop to display custom content anywhere on your site. Get ready to own your WP Loop!
The Scenario
Remembering that content is king, I always recommend to my clients that they have a blog of some sort: a style blog, Pokerblog, Tech blog – whatever it is they do, they need a blog. Thus somewhere in your custom WP theme, you’re going to want to display maybe the latest 2 or 3 articles from their blog. Usually, you could just use query_posts() to bring in posts from a specific category.
However, I’ve found in the past that if I use this method before the normal loop for the page’s content, it messes with the functionality of the main loop because of issues with rewind_posts() and such.
To altogether avoid this issue, it’ll be best if you get used to making your own WP query.
Making your loop
Before looping, you need to tell WordPress what to loop through. So firstly, setup your query.
<?
$recentPosts = new WP_Query();
$recentPosts->query('showposts=5');
?>
Then you can join it up with a loop to process the information that your query finds.
<? while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?> <!-- LOOP STUFF HERE --> <?php endwhile; ?>
So when you add it all together:
<?php
$recentPosts = new WP_Query();
$recentPosts->query('showposts=5');
while ($recentPosts->have_posts()) : $recentPosts->the_post();
?>
<!-- LOOP STUFF HERE -->
<?php endwhile; ?>
The query itself can have many different arguments to help you filter down to exactly what you’re looking for. For example:
cat=3– specify the category that you want to display. If you have more posts that you want to include than exclude, then it might be easier to just filter out the ones you don’t want to see by using:cat=-1, -2, -3etcshowposts=3– specify how many posts you want to showorderby=name– determine how you want your results to be ordered by: author, date, category, id, title, modified, parent and rand. orderby=rand is a great means of having a news area that displays a random news article from the archives.year=2004– display posts from only a specific year
You can string a whole bunch of these arguments together like so:
<? query_posts('cat=3,4,5&showposts=10&orderby=date'); ?>
With a little imagination, your query can retrieve any number of configurable results.



So easy and powerfull!
Thanks for the tip.
Regards
Thank you so much for this! After hours of attempts, this is the only thing that has worked exactly as needed!
I am displaying posts within a page by specific category. I will use this for quite a few different pages so, many, many thanks.
Glad to have helped
How do I loop through categories? For example, I have a static home page, and I want to loop through each category and inside that, display the last 5 posts in that category… “wp_list_categories” is close, but I don’t really want to output links to the categories, any ideas?
you can use this $recentPosts = new WP_Query($query_string . ‘&showposts=10′);
Great, thanks!
How do you make the comments (3 comments, leave a comment) shop up in these loops? Adding the regular comment_popup doesn’t seem to work.
b02y3hpu6s1esm6g
Curious who you could have the post to the right and the post info (author, date, etc) on the left directly next to the title.
http://wordpress.org/support/topic/222740
I wish cutomizing theme wp max, do you may explain me please the folllowing statementes (within showposts)?
Noticias recientes
query(‘showposts=’.$mytheme['latest'].’&cat=-’.$mytheme['video'].’,-’.$mytheme['biblia']); ?>
have_posts()) : $latest->the_post(); ?>
thanks a lot
I’m new to WP – I created a custom field called synopsis for my static pages.
My goal is to create a site map that lists the title [linked] and a short synopsis of the page.
I’m stuck, not understanding how to use a loop …
I’m able to create the link list:
ID.’&echo=0′);
if ($children) { ?>
Or echo the post meta key “synopsis” on the same page as the post:
ID, $key, true); ?>
I want my first loop to include the second output for each iteration.
I struggling with finding a clear example of looping the list of pages getting an ID to output post meta data.
Seems like this would be someting folks would want to do?
Any thoughts?
Will return the sysnopis.
sorry look like the blog stripped the tags. Email me for the full source example of the problem
Man, this is exactly what I need. I’ve just spent all afternoon looking for a solution, and this is perfect. Thanks!
hi, thanks a lot for this nice article, its work for my WP site
Great post!!!
Would you know how to grab the two most recent posts (with excerpt), then show the next three post titles (Just the Titles)?
Thank you!
you just saved me a couple hours of work! Thank you so much.
It looks like i can go grab a coffee!
Hey Josh, what you may be able to do (and this may not be the best way to do it…
you could 1, do a query for just that one post you want the excerpt for
query(‘category_name=news&showposts=1′);
while ($recentPost->have_posts()) : $recentPost->the_post();
?>
<a href=”">Read More»
Then do another query for the next 3 post titles?
query(‘category_name=news&showposts=3′);
while ($onlyTitles->have_posts()) : $onlyTitles->the_post();
?>
You could put that in an include and put it in your header, or wherever you want it. I actually haven’t tested that code, so it may not even work. But that may give you an idea of what to look for?
Hope it helps.
well, it stripped out the php code. email me if you haven’t found out how to do what you’re trying to do.
Not sure why more people don
is there a way to add the category name with the post title
so if it had a category name of category 1 and post titles of post 1 and post 2 it come out as:
category 1 post 1
category 1 post 2
anyway to do that?
nvm i just figured it out…
any idea son how to use a search keyword filter with wp_query? I’m using several custom wp_queries with filters for different authors in place of the loop but it breaks the search and displays everything regardless of the keyword.
Great!
clear and usefull!
you saved my day
THX!
Cool – thanks for this. I’ve been trying to create a another loop in the sidebar and didn’t want to use the query_posts function twice. Cheers
Hi,
I eventually got this
$my_query->query(‘page_id=2
however if i wanted to add more pages $my_query->query(‘page_id=2,3,4 etc
it wont show the other pages just teh first one
please any ideas