Adding ‘menu order’ column to custom post type admin screen

I want to use the menu order attribute to control ordering for a custom post type that’s going to be used for a specific purpose.

It’s easy enough to add this to the CPT via supports => array('page-attributes') but how do I expose the menu order value on the admin listing screen for this CPT?

Related posts

Leave a Reply

4 comments

  1. OK – in the end turned out to be fairly simple – as I’d had some kind of mental block – menu_order is a variable in the $post object (thanks to @brady for reminding me of that).

    @scribu’s post on creating sortable column values then gives the rest.

    So, assuming the custom post type is called header_text, these are the functions and hooks that are needed:

    Add a new column for the order

    /**
    * add order column to admin listing screen for header text
    */
    function add_new_header_text_column($header_text_columns) {
      $header_text_columns['menu_order'] = "Order";
      return $header_text_columns;
    }
    add_action('manage_header_text_posts_columns', 'add_new_header_text_column');
    

    Render the column values

    /**
    * 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_header_text_posts_custom_column','show_order_column');
    

    Set the column to be sortable

    /**
    * make column sortable
    */
    function order_column_register_sortable($columns){
      $columns['menu_order'] = 'menu_order';
      return $columns;
    }
    add_filter('manage_edit-header_text_sortable_columns','order_column_register_sortable');
    
  2. It’s been too long, but just for the record, you can display the ‘menu order’ option in the admin, just by including ‘page-attributes’ in the ‘supports’ option array. For example:

        register_post_type( 'columna',
        array(
            'labels' => array(
                'name' => __( 'Columnas' ),
                'singular_name' => __( 'Columna' ),
            ),
            'supports' => array( 'title', 'thumbnail', 'excerpt', 'page-attributes' ),
            'public' => true,
            'has_archive' => false,
            'menu_position'=>5
        )
    );
    
  3. @anu let me to the right direction, however that is not a modern code

    Modern solution && working on WordPress 5.4

    1. add support
    2. register column
    3. display column value
    4. make the registered column sortable / say by what it should sort

    $MY_POST_TYPE = "flowers"; // just for a showcase
    
    // the basic support (menu_order is included in the page-attributes)
    add_post_type_support($MY_POST_TYPE, 'page-attributes');
    
    // add a column to the post type's admin
    // basically registers the column and sets it's title
    add_filter('manage_' . $MY_POST_TYPE . '_posts_columns', function ($columns) {
      $columns['menu_order'] = "Order"; //column key => title
      return $columns;
    });
    
    // display the column value
    add_action( 'manage_' . $MY_POST_TYPE . '_posts_custom_column', function ($column_name, $post_id){
      if ($column_name == 'menu_order') {
         echo get_post($post_id)->menu_order;
      }
    }, 10, 2); // priority, number of args - MANDATORY HERE! 
    
    // make it sortable
    $menu_order_sortable_on_screen = 'edit-' . $MY_POST_TYPE; // screen name of LIST page of posts
    add_filter('manage_' . $menu_order_sortable_on_screen . '_sortable_columns', function ($columns){
      // column key => Query variable
      // menu_order is in Query by default so we can just set it
      $columns['menu_order'] = 'menu_order';
      return $columns;
    });
    

    If you are interested how would you make sortable column that is not based on some basic Query variable you would play around request filter as described in documentation here: https://make.wordpress.org/docs/plugin-developer-handbook/10-plugin-components/custom-list-table-columns/#sortable-columns