Running Queries From Functions.php (or a plugin)

UPDATE: This tutorial is slightly incomplete. We forgot to limit our query to the main content of the pageonly, to keep it from affecting widgets, menus, etc. See the update here for the rest of the info you’ll need, it’s a simple fix!

I was working on my new company site for The Voodoo Empire. One of the things I had set up was a new members page. It’s a directory of all of our members. They get input as we add new members. So the oldest members (the founders) got input first, and new members would get added as they join. That makes sense. Any new members we add will of course get added as they join. But then on the front end, they display in that same order, newest members on top. Well for a members page, it makes sense to me to have the founding members at the top of the page, with newer members at the bottom. So, how to reverse the order of posts on this page? Here’s a simple solution for that!

My main requirement here, was to be able to sort my posts from functions.php. I’m still working on The Voodoo Empire site. Right now, it’s running the startbox theme. The developer has decided to release it for free, and it’s pretty cool. At some point I’m going to make my own theme, that’s kind of what I do! I like to customize, but until then I had tried many themes before settling on startbox.

Well, how to do my query? See, I could make a child theme, and use query posts in a custom template. That would do exactly what I need, but the problem is, it’s not portable. When I change out themes, the query will be gone. I’ll have to make a new page template in my new theme to accomodate my query.

Why not do my query in a manner that can be dropped into functions.php? If you recall from before, pretty much anything that can be run from functions.php can be run from within a plugin. And lately, we’re all about the functionality plugin. I did a little huntin around the Google machine, and here’s what I came up with for my needs:

function voodoo_member_sort(&$query) {
        if (is_post_type_archive( 've_members' ) ){
        $query->set('order', 'ASC');
add_action('pre_get_posts', 'voodoo_member_sort');

We just need to have some fun with pre_get_posts. We can run all sorts of queries using that. The above code does exactly what I need, but you can go crazy once you know how it works.

You see in line 2, we just set up our function. Line 3 is pretty important. In my example above, I want to sort the archive page for my custom post type ve_members. You can do all sorts of things here using conditional checks. Maybe run the query on just the home page, or anything but the home page. Or like I did check for a custom post type archive. You can combine various things here to.

Then on line 4, I set what I need for my query. I simply needed to sort the posts by ASC, so I only have one line. You may have multiple things, you would just copy that line, and paste it as much as needed, using your different query parameters.

Pretty simple right? In the past, whenever I needed to sort things with custom queries, I would always make another page template. I wish I’d researched this ages ago, no need to make those custom templates. I can keep everything in my functionality plugin, and it’s safe from theme changes, etc. Pretty cool eh?

Leave a Reply