1

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
2
  • Add an ! is_admin() check to your pre_get_posts function. – shanebp Dec 17 '20 at 3:49
  • That did it. Thanks so much. – Andy Dec 17 '20 at 4:24
0

I forgot to add !is_admin() check to my pre_get_posts function. Altered section of code below.

add_action( 'pre_get_posts', 'mpe_portfolio_sort_order'); 
function mpe_portfolio_sort_order($query){
    if(is_archive() && ! is_admin() ):
       $query->set( 'order', 'ASC' );
       $query->set( 'orderby', 'menu_order' );
    endif;    
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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