1

I know this question has likely been asked many times, but I've tried so many supposed solutions to this issue and so far nothing has seem to have worked.

I'm using the Advanced Custom Fields plugin to create a custom dropdown field called statuses to a custom post type st_kb. With the field created I've successfully managed to get a column shown in the admin post section of the dashboard with the field shown in it. However, I would like this to be sortable and so far I have been unable to get it working.

Here is my current code:

function add_column($cols) {
    $cols['bkb_status'] = 'Status';
    return $cols;
}

function add_sort_register_sortable($cols) {
    $cols['bkb_status'] = 'statuses';
    return $cols;
}

function my_custom_columns($column) {
    global $post;

    if ($column == 'bkb_status') {
        if (get_field('statuses') != false) {
            echo get_field('statuses');
        } else {
            echo 'Not set';
        }   
    }
}

add_action("manage_st_kb_posts_custom_column", "my_custom_columns");
add_filter("manage_st_kb_posts_columns", "add_column");
add_filter("manage_st_kb_posts_sortable_columns", "add_sort_register_sortable");

This will show the column in the correct table, but there are no sort arrows available. Please can someone help point out to me what is wrong with this code, or else I may have a plugin conflict?

  • The filter name is slightly off - use manage_edit-st_kb_sortable_columns (source) – TheDeadMedic Nov 11 '15 at 15:29
  • 4 hours of my life wasted, and that was the problem. Thank you! Can you post as an answer? – soup-bowl Nov 11 '15 at 15:33
  • The arrows are here but the actual content is still not being sorted, but I think for that I'm missing the preloader. – soup-bowl Nov 11 '15 at 15:38
2

The filter name is slightly off - use manage_edit-st_kb_sortable_columns. As for the actual sorting not working, you need implement the logic yourself:

function wpse_208315_sort_by_status( $query ) {
   if ( $query->is_main_query() && $query->get( 'orderby' ) === 'statuses' ) {
        $query->set( 'meta_key', 'status' /* Post meta field name of status */ );
        $query->set( 'orderby', 'meta_value' );
   }
}

add_action( 'pre_get_posts', 'wpse_208315_sort_by_status' );
  • 1
    Note that status might not actually be the meta key - this will be the "Field Name" in your ACF settings. – TheDeadMedic Nov 11 '15 at 16:40

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.