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…
You can exclude multple categories like this…
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)…
You can do the same thing to retrieve pages as well like this…
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…
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).
Retrieve Post by Certain Authors
If your blog has multiple authors you can retrive them by name or author id like this…
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.
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.
Using “orderby” there are many different parameters that you can use like these…
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…
You could also get them for the current month and year with a query like this…
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.
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!