Sort CPT archive by order prevents sorting in admin

Question

I’ve activated the Order feature for my ‘portfolio’ CPT and have managed to arrange my ‘portfolio’ posts according to respective Order values I’ve provided.

Unfortunately, I’ve found that this code locks the order that ‘portfolio’ items are displayed in admin; I can’t toggle the Title ascending/descending feature nor am I able to sort this list by Date.

My goal is to display these ‘portfolio’ items on the live site using the Order feature but I’d still like to be able to make use of the Title/Date sorting in admin.

//add order column to admin listing screen
function add_new_portfolio_column($post_columns) {
    $post_columns['menu_order'] = "Order";
    return $post_columns;
  }
  add_action('manage_edit-portfolio_columns', 'add_new_portfolio_column');


//show custom order column values
function show_order_column($name){
    global $post;
   
    switch ($name) {
      case 'menu_order':
        $order = $post->menu_order;
        echo $order;
        break;
     default:
        break;
     }
  }
  add_action('manage_portfolio_posts_custom_column','show_order_column');


//make column sortable
function order_column_register_sortable($columns){
    $columns['menu_order'] = 'menu_order';
    return $columns;
  }
  add_filter('manage_edit-portfolio_sortable_columns','order_column_register_sortable');


//Sort post archive by menu_order
add_action( 'pre_get_posts', 'mpe_portfolio_sort_order'); 
function mpe_portfolio_sort_order($query){
    if(is_archive() ):
       $query->set( 'order', 'ASC' );
       $query->set( 'orderby', 'menu_order' );
    endif;    
};
  • I included ‘page-attributes’ to ‘portfolio’ CPT ‘supports’ array.
  • ‘portfolio’ CPT’s ‘hierarchical’ parameter is set to true.
    enter image description here
0
andy 4 months 2020-12-16T20:10:26-05:00 0 Answers 18 views 0

Leave an answer

Browse
Browse