Modifying the query used by The Loop in Thesis, to use Custom Post Types

Let’s say you use WordPress, and Thesis, and you decided you wanted to add a custom post type (new with WP 3.0!), for whatever reason.  Maybe you just wanted a way to differentiate book reviews from other blog posts, with a custom taxonomy called Authors* and no Categories, because they don’t make sense.

You might do a web search and find these helpful links about how to make the Thesis meta boxes appear in the custom post type’s edit page.  That’s awesome!  But really, I want those posts to show up on my home page and in my RSS feed.  I couldn’t find it documented anywhere.  There are lots of examples of how to narrow down query results in order to show only some of the normal post-type posts, but not on how to add posts that aren’t showing up in the default search.  It turns out it’s actually pretty easy.

So, here’s what you do!

  1. Create your new post type.  I used Custom Post Type UI.
  2. Create a new post of that type.  (Post Type Switcher is a nice way to change an existing post.)
  3. Use the Thesis Custom File Editor to edit custom_functions.php to add the following:
    function custom_post_home($query) {
        if (is_feed() || is_home())
            set_query_var('post_type',array('post','bookreviews'));
    }
    add_filter('parse_query', 'custom_post_home');

What it’s doing:

Line 1 is creating a new function which can be added to a hook.

Line 2 checks if we’re on the home page or in an rss feed, because those are the two places where I expect a mix of posts and bookreviews should appear.

Line 3 updates the query to look for posts of type “post” (the default) or “bookreview”—of course, you would put in your own post type here.

Line 5 adds the custom function to WordPress’s hook “parse_query”.  (You could probably also use “pre_get_posts”, if you wanted.  I didn’t notice a difference in behavior.

——

* To pick an example completely at random.  It could be there is an easier way to accomplish this, but I wanted it as a trial run before creating a custom post type for short fiction.  To see if it’s feasible.  It is, and it’s easy once you know how.

Leave a Reply

Your email address will not be published. Required fields are marked *