Set a Custom Query for Blog/Taxonomy Pages

Set a Custom Query for Blog/Taxonomy Pages

Set Up a Custom Query in WordPressIf you’ve ventured into the wonderful world of custom post types in WordPress, you’ve probably run into the issue of how to order the posts you’ve created. Out of the box, WordPress sorts your blog posts, and custom post types as well, by date, both in the back end and on the front end, on blog, archive, and taxonomy pages.

This can be frustrating if you have a custom post type, or even a category, which you don’t want to have ordered this way. You could fake it by changing the dates of the posts in question, and make it work, but isn’t that a little tedious, and maybe silly, and awkward to explain to a future user, or your Male Chimp intern? You’re right, it is, so this is a quick snippet to show you how to get your posts sorted the way you want them, whatever they are. We are going to set up a custom query for an archive and taxonomy page.

This is not a Genesis Framework specific tutorial; it should work in any WordPress theme, but YMMV on where to put it into your theme or plugin. Also, make sure not to put it into the actual template page, because this code fires early, before the template is even called. It won’t work if it’s in the template.

The Custom Query Code

This is a pretty simple little function which harnesses the power of WP_Query (Codex link). You can do some pretty amazing things with queries, up to and including sorting posts by a custom field (called the meta_value). I’m using an example straight out of the Simple Staff for Genesis plugin I wrote. Here it is:

<?php
/*
* This function is part of Simple Staff for Genesis. Extracted for educational purposes. Could be used also
* in a theme's functions.php file, if you're not working with a plugin. Do not include the opening tag.
*/
add_action( 'pre_get_posts', 'simple_staff_order', 9999 );
function simple_staff_order( $query ) {
if ( $query->is_main_query() && ( is_post_type_archive( 'staff' ) || is_tax( 'department' ) ) ) {
$query->set( 'orderby', 'menu_order' );
$query->set( 'order', 'ASC' );
$query->set( 'posts_per_page', -1 );
}
}

Unpacking the Code

So, here are the highlights of what’s going on. You’ll notice that first, we’re hooking into pre_get_posts, which is why this has to be in your functions.php file or a plugin file, not in the template. This fires before the template.

We need to set a conditional to make sure our query only occurs where we want it to–you don’t want to screw up your blog, for example. So the first part of this conditional is $query->is_main_query, which is checking to see that we’re looking at the main query of the page, not something in a sidebar widget, or navigation, or anywhere else. You can also, and often should, include !is_admin, so that your query is not running on your admin pages, but I chose not to here. If you don’t include that, then the list of the custom post types/staff will be in the same order on both the front and the back end.

&& is PHP speak for AND, so our query here is set up to run only if both the first part and the second part are met. The second part is actually two pieces, so if you count the number of parentheses, you’ll see that the && covers the main query plus either one of the other conditions. The second part is two possible scenarios: 1) if we’re on the staff archive page, || (PHP for OR) 2) if we’re on the custom taxonomy page. So our query runs if we’re looking at the main query AND we’re on the archive page, or we have the main query AND the taxonomy page.

Queries can get pretty complicated, depending on your needs, so make sure all of your parentheses both open and close, or else you can take your site down. Use the Conditional Tags page in the Codex to help you manage your rules.

So now we know when our custom query will run, and the next part tells WordPress what rules we want it to follow. There are a lot of great examples in the Codex. Our rules here are pretty simple:

  1. We’re controlling the order of the staff completely by hand, so orderby is menu_order. Other options could be to order by the slug, if you want posts alphabetized, or meta_value for a custom field.
  2. The order itself is ascending. Posts are usually ordered by date in descending order.
  3. And for my staff page, I’m setting the number of posts_per_page to -1, which tells WordPress to show all of the posts. Obviously this isn’t what you want if you have many posts, but for something like a staff page where you do want them all to show, it makes sense. The default is going to be whatever your site is set to show for blog pages, usually ten.

And seriously, that’s it! This little snippet is really powerful for helping you organize any type of content on your site.

Do something awesome. Tell a friend:

subscribe by email

If you're an email kind of person, that's totally cool. Go ahead and sign up here to get the latest blog entries in your inbox! No worries, I never share your address.
  • This field is for validation purposes and should be left unchanged.

Reader Interactions

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Bars Code Check chevron-down chevron-left chevron-right chevron-up Envelope Laptop Search Times Facebook GitHub Google Plus Instagram LinkedIn Pinterest Twitter WordPress Logo Wordpress Simple