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…
* 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…
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!
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