2 Ways to Reorder Posts from a Specific Category in WordPress (With and Without a Plugin)

Category: WordPress

I had a client that needed to build an archive website for a museum. Needless to say, the museum had thousands of materials, seperated into categories and subcategories. Some of the subcategories needed to have a custom post order on the category archive page (by title or by published date in ascending order), while others should have been ordered with the default post order (date descending). In this tutorial, I will show you two ways how to sort the posts from a certain category archive page like a pro!

Change Post Order for a Certain Category Without a Plugin

If you want to achieve this with a plugin, skip the following paragraph. Now, for those of you, who are not afraid to touch php code, I will show you how you can do a custom post sort. Open your child theme’s functions.php and paste the following snippet:

/* Reverse default date descending order for specific categories */
function change_category_order( $query ) {
    //Sort all interviews by date asc order
    if($query->is_category('19') && $query->is_main_query()){
        $query->set( 'order', 'ASC' );
    }
    //Sort all documents by title and then by date asc order
    elseif($query->is_category('15') && $query->is_main_query()){
        $query->query_vars['orderby'] = 'name';
        $query->query_vars['order'] = 'ASC';
    } 
}
add_action( 'pre_get_posts', 'change_category_order' );

The above code changes the post order for categories with id-s 15 and 19. All the interviews are sorted in a chronological order (from oldest to newest) and all the documents are sorted by title (from A to Z). Do not forget to change the category id-s with the ones you have. You can easily check which is the correct category id by going to the category archive page and hover the mouse on the edit category button in the WordPress admin black top bar (upper right corner).

P.S. If you need a custom sort (for example by location or by date as a custom field) you can accomplish this by using WordPress custom fields. This is a very nice feature that lets you assign custom fields (known as metadata) to posts. They are out of the scope of this article, but you can read more about them here.

Change Post Order for a Certain Category With a Plugin

If you do not want to get your hands dirty with code, you can use a very lightweight plugin that I created, that lets you control the post order for each category you create or edit. This is done from the WordPress dashboard, so it is very user-friendly and convenient and can easily be done by less tech-savy users. In the WordPress dashboard, go to plugins -> add new and install Post Order By Category plugin.

After you have installed the plugin, all you need to do is to edit the category you want to sort. Go to posts->categories and you will notice a new field just below the category description textarea called post order. The dropdown option list allows you to change the post order by date ascending or date descending. Currently, the plugin can reorder posts only by date but in future I might add additional options. For now, If you need more flexibility (sort the posts by title), I suggest you to read the above paragraph as well.

reorder-posts-from-wordpress-admin

Happy blogging!