Wordpress Hack #1 - query_posts
2,469 views -Posted in:
2,469 views
This is the very first post in the 30 Wordpress Hacks in 30 Days Series!
If you use Wordpress - then this is a series you will want to bookmark right now! You might even want to subscribe by email at the top of the page. If you’re like me your sick and tired of googling the crap out of the web trying to find easy ways to hack and customize your Wordpress blog or web site. I’m going to give you a very useful and easy to perform Wordpress hack every day for a month! I think that it’s easier to do something like this than write a humongous post with more information than you’ll never read or digest in one sitting. Instead I’m going to give you one very simple and easy task to do each and every day. Depending on your skill level, you may or may not want to take a look at last week’s post How to Create Your Own Wordpress Theme.
Wordpress Hack #1 - query_posts
Today in our very first Wordpress Hack in our 30-day “Hack-a-thon” we’re going to learn about the Wordpress “template tag” query_posts. You can read about query_posts in the Wordpress Codex here. To use any of these examples you must place the PHP code snippet before “the loop”. If you don’t know what “the loop” is, just follow the link in the previous paragraph to create your own wordpress theme for an explanation. Each and every code snippet can used any any page that uses “the loop”, such as your index.php, category.php, archive.php, tag.php, or search.php.
Exclude posts that belong to a category
Maybe you don’t want posts from a certain category showing up on your index or another archive page. You could even use this to keep certain categories from showing in search results if you needed to. You need to know your category “ID” to do this, just retrieve it from your dashboard under “Manage->Categories”, and the “-1″ with your category ID# like this…
<?php
query_posts('cat=-1');
?>
You can exclude multple categories like this…
<?php
query_posts("cat=-1,-2,-3");
?>
Retrieve a Post or Page
To retrieve a particular post you can either call it by ID (listed in your dashboard) or it’s slug like this (use one line of code or the other, not both)…
<?php
query_posts('p=1'); //using post id
query_posts('name=first-post'); //using post slug
?>
You can do the same thing to retrieve pages as well like this…
<?php
query_posts('page_id=7'); //retrieves page 7 only
query_posts('pagename=about'); //retrieves the about page only
?>
When you use those examples it retrieves the entire post. When you are notorious for writing extremely long posts (like I am) you may only want to get a partial post with the “read more” link. This is especialy useful if you’re going ot feature certain posts on your index or other pages. Here’s the code for that…
<?php
query_posts('p=5'); //get post with id of 5
global $more;
$more = 0; //gets partial post with read more link
?>
You know that you can create pages, and you can also create “child pages” like I have on ths site. I have a parent page called “series”, and then all the individual series index pages are children of that page. If you want to get a child page - that’s possible as well, but you can’t call it by id - you have to call it by double-slug as in the example below (parent slug slash child slug).
<?php
query_posts('pagename=parent/child');
?>
Retrieve Post by Certain Authors
If your blog has multiple authors you can retrive them by name or author id like this…
<?php
query_posts('author_name=John');
query_posts('author=3');
?>
Retrieve Every Single Post
Maybe you want to make some kind of an archive page or sitemap. Whether or not you create a page with this query of course depends on how many posts you really have, but inserting this code before the loop will list ever post you have all on one page. While the code below shows all posts on one page, you can change the “-1″ to just 1 post, or 5, or 10, or however many posts you want to display.
<?php
query_posts('posts_per_page=-1');
?>
Change the Order or Sequence of Posts
By default a Wordpress blog shows you posts in a journal fashion or reverse date order. You could choose to instead sort your posts by author or title like this. You could use either the author or title lines.
<?php
query_posts('orderby=author');
query_posts('orderby=title');
?>
Using “orderby” there are many different parameters that you can use like these…
* orderby=author
* orderby=date
* orderby=category
* orderby=title
* orderby=modified
* orderby=menu_order
* orderby=parent
* orderby=ID
* orderby=rand
Retrieve a Post by Time Period
There are many different ways to consruct a query to retrieve certain posts based by date only. Here’s a way to get them for a day of the month…
<?php
query_posts('day=15'); //all posts on the 15th
?>
You could also get them for the current month and year with a query like this…
<?php $current_month = date('m'); ?>
<?php $current_year = date('Y'); ?>
<?php query_posts("cat=22&year=$current_year&monthnum=$current_month&order=ASC"); ?>
Retrieve Posts based on Tags
You can retrieve posts for a specific tag or tags like this (use one line of code at a time only). The first line retrieves posts with a particular tag, the second line is the format for getting posts for multiple tags, but the third line is for getting posts that were tagged in multiple categories. In other words the second line will retrieve all posts tagged as bread all posts tagged as baking. But the third line will get only posts tagged in bread and baking and recipe.
<?php
query_posts('tag=cooking');
query_posts('tag=bread,baking');
query_posts('tag=bread+baking+recipe');
?>
I hope this helps you do a little Wordpress “theme hacking” and customize your blog. If you have any questions about query_posts please comment now, and we’ll see your tomorrow for the next hack!
Tags: blog-help, tutorial, wordpress-hacks
























April 15th, 2008 at 12:08 am
I had actually been looking for the one about authors.
thanks a lot, it been a big help.
Mark Wilson’s last blog post..I hate the word “nicheâ€
April 15th, 2008 at 9:04 am
Loving the idea even though this one is sort of irrelevant for me, cannot wait for the rest.
April 28th, 2008 at 10:41 pm
Okay… as a wordpress newbie (but someone who chose to dive head-first into WP development… silly me) I can’t tell you how much I value this series that you’re doin! Once I finally get my blog up and running, I’m going to have to toss out a thank-you for helping me so much!
April 29th, 2008 at 7:59 am
@Erika - glad to help! I love comments like this, because it means I made the right decision in starting this series…
August 20th, 2008 at 6:20 pm
[...] — Hacking query_posts in WordPress — In his attempt to present readers with 30 wordpress hacks in 30 days, the first in his [...]
October 6th, 2008 at 9:22 am
thanks for the info guys… appreciate it!
October 14th, 2008 at 8:26 pm
Hi..JT,
It’s an amazing experience for me to come here. Actually, I subscribed to your post by email since last august, but today I decide to join the forum ‘coz I found it’s worthed to stick around here. Hope I can expand my ’skill’ to make profit and contribute here as well.
Blogging for Profits last blog post..Decorating Home Office With Rugs
October 22nd, 2008 at 10:30 am
Great tips, thanks!
What about modifying the query to pull only the latest, say, 45 posts? Or for a date range…?
Thanks!
October 23rd, 2008 at 8:36 am
@Krassy - 45 posts would be query_posts(’posts_per_page=45′); and for a date range read this forum post for the answer and code.![=)]](http://www.jtpratt.com/wp-includes/images/smilies/4.gif)
November 8th, 2008 at 8:03 pm
Hey JT, As usual you seem to really deliver a well thought out post with excellent advice and real help that people need, whether a newbie or blogging for awhile it’s all very useful. Thanks P.S. One question why are there never any dates on your posts, I’ve noticed here that the dates are in the comments but I can’t find them for each post. Or am I missing something, like where’s Waldo? lol
Great post and many thanks
November 12th, 2008 at 10:13 am
@jj-momscashblog - I took the dates out of my permalinks and the meta of my posts a long time ago. I don’t have much use for dates on this blog, as I figure most of my posts should be good for years…![=)]](http://www.jtpratt.com/wp-includes/images/smilies/4.gif)