3

i have created custom post type

my problem is:

in admin column, posts are sorted by ID

how can change it to date. In other words, i want to sort post by recently

5
  • I hope you do know that post ids are set to autoincrement and the oldest post will have the least id and so on. So unless you have some system that checks the missing post ids (of the ones deleted) and switches off Auto Increment and then adds new posts with old ids, the question doesn't stand. Mar 2, 2015 at 11:06
  • 1
    @Ehsan - Unless I'm losing the plot, posts are by default sorted by date when viewing them in the Admin area. If that is not what you want, most of the columns are sortable by clicking on the column header.
    – David Gard
    Mar 2, 2015 at 16:51
  • @SaurabhShukla - Users can edit the publish date of a Post after it has been created, so if Posts were to be ordered by ID (which they are not by default) then they wouldn't necessarily be in date order.
    – David Gard
    Mar 2, 2015 at 16:52
  • @DavidGard Oh, that I didn't consider in this context thanks to the description I have. Mar 2, 2015 at 17:02
  • 2
    @DavidGard tanks. my problem is that you told about this: "losing the plot, posts are by default sorted by date when viewing them in the Admin area"
    – Ehsan
    Mar 3, 2015 at 5:14

2 Answers 2

1

You must add this part to function.php:

add_action( 'pre_get_posts', 'example_func', 1 );
  function example_func( $query ) {
   if ( is_admin() && $query->is_main_query() ) {
     $query->set( 'order' , 'DESC' );
   }
   return $query;
}
1
2
add_action( 'pre_get_posts', 'change_post_sort', 1 );
function change_post_sort( $query ) {
        if ( is_admin() && $query->is_main_query() ) {
            $query->set( 'order' , 'DESC' );
            $query->set( 'orderby', 'modified');
    return $query;
}
1
  • Please add some explanation to the code snippet, thanks.
    – birgire
    Feb 4, 2019 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.