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

Custom Code

Website Development

WooCommerce Stores

Maintenance Plans

Compliance Services

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 blogger. He is the founder of WP Mayor, the plugins WP RSS Aggregator and Spotlight, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com.

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

More from our blog...

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

Post a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Stay updated with WP Mayor's newsletter showcase every week

Stay on top of every new WordPress innovation and latest launches. Receive all our fresh product reviews and expert guides directly in your inbox.

Hosting Survey 2024

Are you happy with your hosting provider or are you over-paying for too little? Have your say below!

"*" indicates required fields

What's the main reason you picked this host?*
How happy are you with your host?*

OPTIONAL: If you'd like to receive the results of this survey, please enter your details below.