Support

Account

Home Forums Backend Issues (wp-admin) WordPress admin column sort by date

Helping

WordPress admin column sort by date

  • We are working on a project with a cpt and a date field for events.
    My clients wants to order the events by date in the admin columns.

    add_action( 'pre_get_posts', 'posttype_default_order' );
    function posttype_default_order( $query ) {
      if( ! is_admin() || ! $query->is_main_query() ) {
        return;
      }
      
      if ( 'when' === $query->get( 'orderby') ) {
        $query->set( 'orderby', 'meta_value_num' );
        $query->set( 'meta_key', 'when' );
      }
    }

    But this does not work. Because we want to order this way:

    1. 2020-01-01
    2. 2020-05-02
    3. 2020-06-11
    4. 2020-06-18
    5. 2019-05-02
    6. 2019-06-11
    7. 2018-03-08
    8. etc. etc. etc.

    I’m not able to get this sorting correct.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.