WordPress

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.
Table of Contents

If you purchase through a link on our site, we may earn a commission.

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 is an investor, entrepreneur, and writer. He is the founder of WP Mayor, and the plugins WP RSS Aggregator and Spotlight. He also runs AgentVania. Connect with him on X or LinkedIn, or visit jeangalea.com.

If you purchase through a link on our site, we may earn a commission.

6 Responses

  1. Wow. Interesting. Thanks.

    Question: Can this be used to sort by a post metadata field / value?

    If so, can you add that example above?

  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:

    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!

      1. HEEEEEEEELP! I’m WAY out of my depth here! Where do I find the functions.php file? Do I have to upgrade from .com?

  3. 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

More from Our Blog

Get the Monthly Showcase

One email a month, written by me, with the best of the last month’s editorial work and some stuff you won’t find anywhere else.

Stay curious and keep building.

Mark from WP Mayor

Who reads WP Mayor? Help us find out. Take the survey.