WordPress How to add Admin Custom Column Sorting by display value

In WordPress, is there a way to make a custom column sortable by the display value instead of the meta_value? What I mean is that my meta value is a number, but I’ve manipulated that number with an existing hook for it to be a string. I’d like to sort by that string instead of the number. This is my existing, working code that will sort it just fine – it just won’t be in alphabetical order:

add_action('manage_resource_posts_custom_column', 'my_manage_resource_columns', 10, 2);
function my_manage_resource_columns($column_name, $id) {
    switch ($column_name) {
    case 'type':
        $id = get_the_ID();
        $par = get_field('resource_type',$id);
        $type = get_field('singular_name',$par);
        echo $type;
            break;
    default:
        break;
    }
} 

add_filter( "manage_edit-resource_sortable_columns", "my_last_modified_column_register_sortable" );
function my_last_modified_column_register_sortable( $columns ) {
    $columns["type"] = "type";
    return $columns;
}

add_filter( "request", "sort_column_by_modified" ); 
function sort_column_by_modified( $vars ){
    if ( isset( $vars["orderby"] ) && "type" == $vars["orderby"] && $_GET['post_type']=='resource') {
        $vars = array_merge( $vars, array(
            'meta_key' => 'resource_type',
            'orderby' => 'meta_value_num'
        ) );
    }
    return $vars;
}

So you can see where I add the actual text for the column name based on the numeric value in the database. It displays fine. I just need it to be sortable by that text and not the value it represents. Does anyone know if this is possible / how to do it?

Read More

Here is a screen shot of the column as it is now. You can see that they are indeed grouped up by type, but its by numerical order of the ID

enter image description here

Thanks in advance!

Related posts

Leave a Reply

1 comment

  1. If you want to sort a column on more than one meta value, you will use the meta_query argument for your sort algorithm. To give an example as to when you would want to sort on more than one parameter. Lets say you you have a list of TV shows listed by Season and Episode. To sort by Episode alone would put all the Episodes 1 together, etc., but what really want, when sort by Episode to also to sort by Season-Episode, as this is more readable and understanding to the admin user.

    To accomplish this, in the sort_column_by_modified function, replace

    $vars = array_merge( $vars, array(
            'meta_key' => 'resource_type',
            'orderby' => 'meta_value_num'
        ) );
    

    with

    $vars = array_merge( $vars, array(
            'meta_query' => array(
                  array(
                      'meta_key' => 'season',  // the column name
                      'order_by_num' => 'meta_value_num'
                  ),
                  array(
                      'meta_key' => 'episode', // the column name
                      'order_by_num' => 'meta_value_num'
                  ),
             )
        ) );
    

    For other types of sorts, adjust your meta_query appropriately.