Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Wordpress, how do I set the default admin sort order for a custom post type to a custom column?

Tags:

I've set up a custom post type called clientarea, and set up several custom columns for it in the admin area - the custom columns are all custom meta fields, as you can see from my code. I'd like to sort by 'Appointment Date' descending by default.

All of the columns work fine, and can be sorted manually as expected, but I can't get the default sort order to work.

If I change the default sort field to a standard field (e.g. 'title') it works as expected; it only seems not to work when I'm trying to set a custom column as the default sort order. The order works (i.e. I can change between asc and desc by default even with a custom column), but it's not picking up the orderby so is reverting back to sorting by the date the custom post was published.

What am I missing?

My code is as follows:

add_action( 'manage_posts_custom_column' , 'custom_columns', 10, 2 );  function custom_columns( $column, $post_id ) {     global $wpdb;     switch ( $column ) {         case 'extranet_case_office':             $get_office_ID = get_post_meta( $post_id, 'extranet_case_office', true );             $get_office_name = $wpdb->get_results('SELECT post_title FROM `cn_bf_posts` WHERE `ID` = '.$get_office_ID);             echo $get_office_name[0]->post_title;             break;         case 'extranet_appointment_date':             echo date('d/m/Y',strtotime(get_post_meta( $post_id, 'extranet_appointment_date', true )));              break;         case 'extranet_appointment_type':             echo get_post_meta( $post_id, 'extranet_appointment_type', true );              break;         case 'extranet_insolvency_practioner':             $get_person_ID = get_post_meta( $post_id, 'extranet_insolvency_practioner', true );             $get_person_name = $wpdb->get_results('SELECT post_title FROM `cn_bf_posts` WHERE `ID` = '.$get_person_ID);             echo $get_person_name[0]->post_title;             break;         default:             break;     } }  add_filter( 'manage_edit-clientarea_sortable_columns', 'my_sortable_clientarea_columns' );  function my_sortable_clientarea_columns( $columns ) {     $columns['extranet_case_office'] = 'extranet_sort_office';     $columns['extranet_appointment_date'] = 'extranet_sort_date';     $columns['extranet_appointment_type'] = 'extranet_sort_type';     $columns['extranet_insolvency_practioner'] = 'extranet_sort_IP';     return $columns; }  add_action( 'pre_get_posts', 'extranet_orderby' );  function extranet_orderby( $query ) {     if( ! is_admin() )         return;      $orderby = $query->get( 'orderby');      switch ( $orderby ) {         case 'extranet_sort_office':             $query->set('meta_key','extranet_case_office');             $query->set('orderby','meta_value_num');             break;         case 'extranet_sort_date':             $query->set('meta_key','extranet_appointment_date');             $query->set('orderby','meta_value');             break;         case 'extranet_sort_type':             $query->set('meta_key','extranet_appointment_type');             $query->set('orderby','meta_value');             break;         case 'extranet_sort_IP':             $query->set('meta_key','extranet_insolvency_practioner');             $query->set('orderby','meta_value_num');             break;         default:             break;     } }  add_action('pre_get_posts','clientarea_default_order'); function clientarea_default_order( $query ){     if( $query->get('post_type')=='clientarea' ){         if( $query->get('orderby') == '' )             $query->set('orderby','extranet_sort_date');          if( $query->get('order') == '' )             $query->set('order','desc');     } } 
like image 758
Sinister Beard Avatar asked Jul 15 '15 15:07

Sinister Beard


People also ask

How do I change the administrative post list in WordPress?

The filter for modifying, removing or adding columns to post list in WordPress admin panel is manage_{$post_type}_posts_columns . Exchange {$post_type} with the desired post type. For example; if you want to edit columns for post type ' post ', the filter name would be manage_post_posts_columns .

How do I add an admin to a custom field in WordPress?

Adding Custom Fields in WordPress First, you need to edit the post or page where you want to add the custom field and go to the custom fields meta box. Next, you need to provide a name for your custom field and then enter its value. Click on the Add Custom Field button to save it.


2 Answers

The problem is that you run the clientarea_default_order callback too late.

To fix that you only have to change the priority from the default one that's 10:

add_action( 'pre_get_posts','clientarea_default_order'); 

to the priority of 9:

add_action( 'pre_get_posts','clientarea_default_order', 9 ); 

But you don't actually need two pre_get_posts callbacks.

You can combine them:

Example #1

is_admin() && add_action( 'pre_get_posts', 'extranet_orderby' );      function extranet_orderby( $query )  {        // Nothing to do:       if( ! $query->is_main_query() || 'clientarea' != $query->get( 'post_type' )  )         return;      //-------------------------------------------       // Modify the 'orderby' and 'meta_key' parts     //-------------------------------------------       $orderby = $query->get( 'orderby');            switch ( $orderby )      {         case 'extranet_sort_office':             $query->set( 'meta_key', 'extranet_case_office' );             $query->set( 'orderby',  'meta_value_num' );             break;         case 'extranet_sort_date':             $query->set( 'meta_key', 'extranet_appointment_date' );               $query->set( 'orderby',  'meta_value' );             break;         case '':  // <-- The default empty case             $query->set( 'meta_key', 'extranet_appointment_date' );               $query->set( 'orderby',  'meta_value' );             break;         case 'extranet_sort_type':             $query->set( 'meta_key', 'extranet_appointment_type' );             $query->set( 'orderby',  'meta_value' );             break;         case 'extranet_sort_IP':             $query->set( 'meta_key', 'extranet_insolvency_practioner' );             $query->set( 'orderby', 'meta_value_num' );             break;         default:             break;     } } 

where we added a main query check and an empty switch case.

Example #2

Here's another approach, without the switch part (PHP 5.4+):

is_admin() && add_action( 'pre_get_posts', 'extranet_orderby' );  function extranet_orderby( $query ) {     // Nothing to do     if( ! $query->is_main_query() || 'clientarea' != $query->get( 'post_type' )  )         return;      //-------------------------------------------       // Modify the 'orderby' and 'meta_key' parts     //-------------------------------------------       $orderby = strtolower( $query->get( 'orderby') );       $mods = [         'office' => [ 'meta_key' => 'extranet_sort_office',           'orderby' => 'meta_value_num' ],         'date'   => [ 'meta_key' => 'extranet_appointment_date',      'orderby' => 'meta_value'     ],         ''       => [ 'meta_key' => 'extranet_appointment_date',      'orderby' => 'meta_value'     ],         'type'   => [ 'meta_key' => 'extranet_sort_type',             'orderby' => 'meta_value_num' ],         'ip'     => [ 'meta_key' => 'extranet_insolvency_practioner', 'orderby' => 'meta_value_num' ],     ];     $key = 'extranet_sort_' . $orderby;     if( isset( $mods[$key] ) )     {         $query->set( 'meta_key', $mods[$key]['meta_key'] );         $query->set( 'orderby',  $mods[$key]['orderby']  );     } } 
like image 189
birgire Avatar answered Sep 21 '22 21:09

birgire


Try change clientarea_default_order action & function for this:

add_action('pre_get_posts', 'clientarea_default_order', 99);  function clientarea_default_order($query) {   if ($query->get('post_type') == 'clientarea') {     if ($query->get('orderby') == '') {         $query->set('orderby', 'extranet_sort_date');         $query->set('meta_key', 'extranet_appointment_date');     }     if ($query->get('order') == '') {         $query->set('order', 'DESC');     }   } } 
like image 35
Martin Hlaváč Avatar answered Sep 21 '22 21:09

Martin Hlaváč