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: 30 wordpress hacks in 30 days series, blog-help, build a wordpress query, create your own wordpress theme, exclude multiple catogories using query_posts in wordpress, how to create your own wordpress theme, http://www.jtpratt.com/wordpress-hack-1-query_posts/, jtpratt, jtpratt.com, multiple query_posts, multiple query_posts wordpress, pagename=parent/child, php query_posts, php query_posts order by, posts_per_page=-1, query_post sorting hacks for wordpress, query_posts, query_posts child, query_posts child pages, query_posts children, query_posts comments, query_posts date range, query_posts default order, query_posts examples, query_posts exclude post id, query_posts id, query_posts in wordpress, query_posts menu_order, query_posts meta, query_posts on a page, query_posts order, query_posts order by date, query_posts orderby, query_posts orderby date, query_posts pages, query_posts post id, query_posts posts_per_page, query_posts retrieve the child page, query_posts search, query_posts search like, query_posts search page, query_posts slug, query_posts template, query_posts title, query_posts to show one specific post, query_posts tutorial, query_posts wordpress, query_posts wordpress date range, query_posts wordpress parameters, query_posts(catid=1, tutorial, Wordpress, wordpress category by date range, wordpress codex query_posts, wordpress date range, wordpress multiple query_posts, wordpress orderby multiple, wordpress orderby=author, wordpress orderby=parent, wordpress popular posts date range, wordpress popular posts within date range, wordpress query, wordpress query author, wordpress query by date, wordpress query date range, wordpress query order by, wordpress query posts, wordpress query posts for one post, wordpress query_posts, wordpress query_posts cat error, wordpress query_posts category, wordpress query_posts child, wordpress query_posts current month, wordpress query_posts date, wordpress query_posts exclude id, wordpress query_posts month, wordpress query_posts order, wordpress query_posts order by category, wordpress query_posts orderby, wordpress query_posts page change, wordpress query_posts pagename, wordpress query_posts parameters, wordpress query_posts posts_per_page, wordpress query_posts search, wordpress query_posts single post, wordpress query_posts title, wordpress query_posts() child of, wordpress query_posts() use in page, wordpress search query_post, wordpress show posts by date range, wordpress-hacks, wordpress-theme









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â€
Loving the idea even though this one is sort of irrelevant for me, cannot wait for the rest.
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!
@Erika – glad to help! I love comments like this, because it means I made the right decision in starting this series…
thanks for the info guys… appreciate it!
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
Great tips, thanks!
What about modifying the query to pull only the latest, say, 45 posts? Or for a date range…?
Thanks!
@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.
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
@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…
It’s great to see your series of posts -well after you started, though
it’s what I’ve been looking for and, as you pointed out, its hard to find decent tutorials for WP newbies.
I’m considering switching from MT to WP after over two years of using that CMS… mainly because I find so many features and plugins for WP that are not available to those without enought time or skills to hack it. Would you recommend it or what do you think?
Regards.
Francisco Aguirre´s last blog ..The therapeutic action of Dermune cream on dermatitis, lichen planus, and pustulous psoriasis
nice hacks… very helpful!
thanks a lot
Hi,
thanks for this excellent tutorial. I have been here 5 minutes and already learned a ton of stuff.
I have a question. Using the pressplay theme, each post has a (i don;t know what to call it)…banner? header?…anyway, it’s the thing above the post that contains the category, the byline, and the date. I would like to exclude CERTAIN categories from showing up there, but absolutely cannot figure out how to do this. I know the category id numbers (thanks to this tutorial) but don’t know what I am hoping is simple code, and also where to put this code. Any ideas?
I’m sure I’ll be back with more questions…no matter how nice it is ‘out-of-the-box’ it seems there is always something to change….
Thanks in advance,
Debbie
I’m sorry, I don’t currently have time to give you a direct solution, but if you learn the code it’s certainly doable. Google the codex for solutions.
how do I exclude pages completely?
How about trying the Exclude Pages plugin?
I have been using Wordpress for a few months now. I’m diligently trying to learn all I can. I really appreciate it when I come across information such as you’ve presented here. I’m going to study your “How to create your own WP theme”. I don’t necessarily want to create my own theme, I just want a better understanding of what I’m looking at when I try to edit. Thanks for the help.
Thanks,
Susan
Susan@Best Bread Machine´s last blog ..Panettone Christmas Bread
Hey There, GREAT site, excellent tutorials.
I use WordPress every day, so reading your stuff on this post is just a big help for me.. Also I have been looking for sites like this for some time now, and i find that most of the ones i find is not as serious as yours.
So tank you a lot..
Best Regards