How to Change Order or Posts or Custom Post Types in the Dashboard

In the WordPress dashboard, posts are displayed in the 'latest first' order. This also applies to pages and custom post types. However it is also easy to change this order if you wish to do so. It might be more practical for example, to have them display according to their title, in ascending order.
Our Sponsors
Essential plugins for your WooCommerce store
Build unbuildable WordPress websites
The best no-code Instagram feed plugin for WordPress

In the WordPress dashboard, posts are displayed in the ‘latest first’ order. This also applies to pages and custom post types. However it is also easy to change this order if you wish to do so. It might be more practical for example, to have them display according to their title, in ascending order.

This is how you do it, using the pre_get_posts filter:

[php]
add_action( ‘pre_get_posts’, ‘mycpt_order’ );
/**
* Change order of custom post type to alphabetical ascending
*/
function mycpt_order( $query ) {
// check if we’re in admin, if not exit
if ( ! is_admin ) {
return;
}

$post_type = $query->get(‘post_type’);

if ( $post_type == ‘mycpt’ ) {
/* Post Column: e.g. title */
if ( $query->get( ‘orderby’ ) == ” ) {
$query->set( ‘orderby’, ‘title’ );
}
/* Post Order: ASC / DESC */
if( $query->get( ‘order’ ) == ” ){
$query->set( ‘order’, ‘ASC’ );
}
}
}

[/php]

Of course, replace mycpt with the name of your post type.

If you enjoyed this post, make sure to subscribe to WP Mayor’s RSS feed.

 

Jean Galea
Jean Galea
Jean Galea is a WordPress developer, entrepreneur and padel player. He is the founder of WP Mayor, the plugins WP RSS Aggregator and EDD Bookings, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com.

Consider sharing this post so others can find it:

Share on facebook
Share on twitter
Share on linkedin
Share on reddit
Share on telegram
Share on whatsapp
Share on pocket
Share on email

Join thousands of people receiving real-world, genuine evaluations of WordPress products and services just like this one every week.

Table of Contents

5 Responses

  1. Is there an easy way to display posts from first to last, rather than the last up front? I ask because pleasepresshere.wordpress.com basically turned into a sort-of book, and it struck me that WordPress might further the e-book cause by adding such a feature.

    Thanks,

    3

  2. Yes there is an easy way, if you don’t mind pasting some code into your functions.php file, or creating a simple plugin that does this.

    The WordPress query has many parameters, and one of them controls the order in which posts are displayed, as indicated above. You need to take a look at this link: http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

    Basically you will need to set the orderby to ‘date’ and order to ‘DESC’.

    1. Hmm! I understand most of those words. I’ll take a look at the link! Can I come back when I don’t understand it?? 😉

      Thanks for your help!

Leave a Reply

Your email address will not be published. Required fields are marked *

The Beginner’s Handbook
From an introduction on how WordPress works to our recommendations on products and services.

Join thousands of people receiving real-world, genuine evaluations of WordPress products and services

A single weekly email directly to your inbox.