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?

Published by

Rev. Voodoo

Rev. Voodoo is the co-founder and owner of The Voodoo Empire. The Voodoo Empire is a variety of things. As a business we handle simple web and graphic design. We officiate weddings of any type. We have a e-commerce storefront where we sell Voodoo Empire branded items, along with items for our artists, and for pretty much anyone. We sell your items, or help design items to get up for sale. Our focus though, what got us started in the first place, is music. We try to go out of our way to help the electronic music community in any way possible. Be it through promotion, music hosting, distribution, or assistance with design. Rev. Vooodoo runs The Voodoo Empire's web presence. Setting up websites, getting things online, whatever it takes to get the Empire out there and heard.

Leave a Reply