filters – Creating sortable columns in admin tables for multiple custom post types not working

Question

I have two custom post types named professor and campus.
To each of these CPT’s I have added two custom admin columns named image and last_modified, which currently display correctly on both CPT’s admin tables.

I have also filtered each CPT by post_type, however the manage_edit-post_sortable_columns is not displaying the titles as sortable in the admin tables.

I thought if I used manage_edit-post_sortable_columns I could target the ones I have conditionally checked for?

Am I missing something?

Here is the code:

// add featured image and last modified columns to cpt admin tables (works correctly)
function cpt_columns($columns) {
    global $current_screen;
    if (in_array($current_screen->post_type, array('professor', 'campus'))) {
        $columns['last_modified'] = 'Last Modified';
        $columns['post_thumbs'] = 'Image';
    }
    return $columns;
}
add_filter('manage_posts_columns', 'cpt_columns');


// custom columns as sortable (not working)
function cpt_columns_sortable($columns) {
    global $current_screen;
    if (in_array($current_screen->post_type, array('professor', 'campus'))) {
        $columns['last_modified'] = 'Last Modified';
        $columns['post_thumbs'] = 'Image';
    }
    return $columns;
}
add_filter('manage_edit-post_sortable_columns', 'cpt_columns_sortable');


// output custom column content
function cpt_columns_content($column_id, $post_id) {
    if ($column_id == 'last_modified') {
        echo get_post_field('post_modified', $post_id);
    } else if ('post_thumbs') {
        echo the_post_thumbnail('xsm_thumbnail');
    }
}
add_action('manage_posts_custom_column', 'cpt_columns_content', 10, 2);

0
Krys 5 months 2022-03-08T03:16:01-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse