0

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.

enter image description here

// (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;
}
0

I just realized that there was a typo in the code. The meta-tag should read 'prod_hierar' instead of 'prod-hier'. This fixes the issue. (Too much experimenting, I guess ..).

Function 'wms_hierarchy_col_orderby()' thus becomes:

function wms_hierarchy_col_orderby( $vars ) {
    if ( isset( $vars['orderby'] ) && 'prod_hierar' == $vars['orderby'] ) {
        $vars = array_merge( $vars, array(
            'meta_key' => 'prod_hierar',
            'orderby' => 'meta_value'
        ) );
    }
    return $vars;
}

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.