Custom WordPress Queries

thesis-tips

At the bottom of my individual posts in Thesis Tips, I used to have a list of all my Thesis Tips This just checks to see if the current post is tagged ‘thesis-tip’, and that it’s not on the homepage, and if it meets those criteria then it lists all Thesis Tips.

I use this technique for lots of things. It can show the most recent posts in the footer, list of certain posts on specific pages… any time you want to call WordPress and not mess with the main content (called “The Loop”).

The Query

For information on all the parameters you can use, see WP_Query in the Codex.

For my example, I’m going to get all the posts tagged ‘thesis-tip’ and put them in a list with just the title hyperlinked to the post itself.

Make sure you include the wp_reset_query(), which will (as the name implies) reset the query back to the default. If you don’t do this, WordPress will think the current page is whatever last item was in your new query, which will cause lots of issues (ex: comments from the wrong post showing up).

Bill Erickson is a WordPress Consultant who builds custom websites using WordPress as a CMS and the Genesis framework. He contributes to the WordPress community through free themes, plugins, tutorials, and core patches. He's also a cofounder of the BIL Conference (the open analog to the TED Conference).

Looking for more great tutorials? See them all!

Comments

  1. Leaf says:

    Could this work with Sticky posts also?

  2. Sam Hooker says:

    This worked great for a custom taxonomy I needed to pull onto a Featured Portfolio page. Thanks, Bill — hail to the king :)

Trackbacks

  1. [...] Follow this link: Custom WordPress Queries — Bill Erickson [...]

Speak Your Mind

*

If you'd like to include code in your post, please post it to http://gist.github.com and include a link.