Issue with sortable columns in the ‘Pages’ dashboard overview

enter image description here

I would like to alphabetically order pages in the ‘Pages’ dashboard either by ‘title’ or a custom column (‘hierarchy’). Basically all pages are assigned a hierarchy code (1, 1.1, 1.1.01, 1.1.02, etc.). When I click on ‘hierarchy’ sort button, the result is incorrect.

Can someone give me a hint or point me to a resource that discusses this issue? My code is listed below.

// (Works.) Make custom columns 'prod-hierar' and 'slug' sortable
add_filter( 'manage_edit-page_sortable_columns', 'wms_sort_cust_cols', 10, 2 );

function wms_sort_cust_cols( $col ) {
    // Make column 'hierarchy' sortable ie add sorting icon

    $col[ 'hierarchy' ] = 'prod_hierar';

    return  $col;
}


// (Does not work.) Order column 'hierarchy' alphabetically via meta_key 'prod_hier'.
add_filter( 'request', 'wms_hierarchy_col_orderby' );

function wms_hierarchy_col_orderby( $vars ) {
    PC::debug('wms_hierarchy_col_orderby() hit');
    if ( isset( $vars['orderby'] ) && 'prod_hier' == $vars['orderby'] ) {
        $vars = array_merge( $vars, array(
            'meta_key' => 'prod_hier',
            'orderby' => 'meta_value'

            //$query->set( 'meta_key', 'prod_hier' );
            //$query->set( 'orderby', 'meta_value' );
        ) );
    }
    return $vars;
}

Read more here:: Issue with sortable columns in the ‘Pages’ dashboard overview

Leave a Reply

Your email address will not be published. Required fields are marked *