apply_filters( “manage_{$this->screen->id}_sortable_columns”, array $sortable_columns )

Filters the list table sortable columns for a specific screen.

Description

The dynamic portion of the hook name, $this->screen->id, refers to the ID of the current screen.

Parameters

$sortable_columnsarray
An array of sortable columns.

Source

$_sortable = apply_filters( "manage_{$this->screen->id}_sortable_columns", $sortable_columns );

Changelog

VersionDescription
3.1.0Introduced.

User Contributed Notes

  1. Skip to note 5 content

    As an example. When you see the table with the posts in the admin screen, you can sort the posts by title. If you wanted to remove the ability to sort by the title you could do the following:

    The screen ID for the posts overview page in the admin is edit-post, so the filter would be “manage_edit-post_sortable_columns”.

    add_filter( 'manage_edit-post_sortable_columns', 'slug_title_not_sortable' );
    function slug_title_not_sortable( $cols ) {
    	unset( $cols['title'] );
    	return $cols;
    }
  2. Skip to note 6 content

    For the list of custom type, the hook name is a COMBINATION of edit and the custom post type, so let’s say you have custom post type flowers and you want to add sorting by e.g. menu_order you’ve added before, the hook would be called “manage_edit-flowers_sortable_columns’ – the hook code could look something like:

    $my_post_type = 'flowers';
    
    // actually a screen name of the LIST page of posts
    $menu_order_sortable_on_screen = 'edit-' . $my_post_type;
    
    add_filter(
       'manage_' . $menu_order_sortable_on_screen . '_sortable_columns', 
       function( $columns ) { 
          $columns['menu_order'] = 'menu_order';
          return $columns;
       }
    );

    If you are interested how would you add the menu_order support to a post type in the first place:

    // the basic support
    add_post_type_support( $my_post_type, 'page-attributes' );
    
    // add a column to the post type's admin
    add_filter( 'manage_' . $my_post_type . '_posts_columns', function( $columns ) {
      $columns['menu_order'] = __( 'Order', 'textdomain' ); 
      return $columns;
    } );
    
    // display the column value
    add_action( 'manage_' . $my_post_type . '_posts_custom_column', function( $column_name, $post_id ) {
      if ( 'menu_order' === $column_name ) {
        echo __( get_post( $post_id )->menu_order, 'textdomain' );
      }
    }, 10, 2 ); // priority, number of args - MANDATORY HERE! 
    
    // ... and make it sortable with the code from the start
  3. Skip to note 8 content

    As of WordPress version 6.3.0, the $sortable_columns parameter is now a multidimensional array where you can also now specify abbreviations, orderby text, and the initial sorting order. For example, this is what is dumped for the edit-post screen:

    array (size=4)
        'title' => array (size=4)
            0 => string 'title' (length=5)
            1 => boolean false
            2 => string 'Title' (length=5)
            3 => string 'Table ordered by Title.' (length=23)
        'parent' => array (size=2)
            0 => string 'parent' (length=6)
            1 => boolean false
        'comments' => array (size=4)
            0 => string 'comment_count' (length=13)
            1 => boolean false
            2 => string 'Comments' (length=8)
            3 => string 'Table ordered by Comments.' (length=26)
        'date' => array (size=5)
            0 => string 'date' (length=4)
            1 => boolean true
            2 => string 'Date' (length=4)
            3 => string 'Table ordered by Date.' (length=22)
            4 => string 'desc' (length=4)

    Ignoring the parent column, both title and comments demonstrate what a standard sortable column looks like. The date column is initialised to display the posts in descending order.

    Using jave.web’s example from 4 years ago, the code could now look like this:

    $my_post_type = 'flowers';
    
    // actually a screen name of the LIST page of posts
    $menu_order_sortable_on_screen = 'edit-' . $my_post_type;
    
    add_filter(
        'manage_' . $menu_order_sortable_on_screen . '_sortable_columns',
        function ( $columns ) {
            $columns['menu_order'] = array(
                'menu_order', // Menu's internal name, same as key in array
                true, // Initialise with my specified order, false to disregard
                __( 'Menu Order' ), // Short column name (abbreviation) for `abbr` attribute
                __( 'Table ordered by menu order.' ), // Translatable string of a brief description to be used for the current sorting
                'asc' // Initialise in ascending order, can also be 'desc', false, or omitted to default to false
            );
            return $columns;
    } );

You must log in before being able to contribute a note or feedback.