Custom Ordering of WordPress posts.

01/16/2009

I seem to be becoming the go-to guy for WordPress in my circle of friends. I don't mind—I've grown fond of pulling apart other people's code, and the monster that WordPress is always makes that interesting.

Last night, I was asked if I know how to order posts differently in The Loop. I don't trust The Loop. I much prefer to use WP_Query, as I feel more in control, but my friend had already written a lot of functions that seemed to depend on using The Loop, and I've never been one for rocking the boat where live code is concerned! <!--more--> So, my buddy has the current code and it's not working:

queryposts($querystring . '&order=asc&orderby=title');

The more perceptive will spot that he's trying to order his posts by title, rather than by date, but no matter how you order/rename the variables, the results come back the same. A few minutes hunting later leads me to a query string variable called suppressfilters, which is called at the last minute in the query build, which applies things like ordering for stickies. Obviously, if you're running a custom query, chances are you don't care about stickies, so simply set suppressfilters to true when calling query_posts() :

queryposts($querystring . '&order=asc&orderby=title&suppress_filters=true');

and the posts in your loop will be ordered the way you told them to be ordered. I'm pretty sure that there's a point for this in the grand scheme of things, but I'm damned if I can see it.