Change Post Order in WordPress

A query from Marco asked if I had some code that would change the order of posts in a category page that displayed a list of lessons. I didn’t, but it didn’t take long to find and write-up.

Add the following to your functions.php file:

add_action('wp_head', 'custom_change_posts_order');
function custom_change_posts_order() {
    global $query_string;
    if (is_category('corsi')) {
        $posts = query_posts( $query_string . '&orderby=title&order=asc' );
    }
}

It’s that simple. As Marco and anyone else familiar with WordPress code will guess, this is very powerful. The conditional logic on what it applies to can be changed for some other conditional tags, while the query_posts parameters offer lots of functionality too.

Comments

  1. Marco says:

    Many thanks Gary, now I don’t have to hassle about the order I have to post those lessons. It saved me a lot of time! And also is useful to prevent many calls from clients…

  2. nsp-code says:

    You can do that manually and set any order you need using this plugin Post Types Order

Speak Your Mind

*