sortable columns for multiple custom post types not working

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);

Topic wp-list-table filters admin hooks custom-post-types Wordpress

Category Web


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

That hook runs only on the "Posts" page (at wp-admin/edit.php) for managing posts in the default/core post post type. I.e. When the post_type parameter in the (current page) URL is not set or empty, or that it's set to post.

For other post types like page and your campus CPT, use edit-<post type> in place of edit-post.

So replace the add_filter('manage_edit-post_sortable_columns', 'cpt_columns_sortable'); with:

add_filter('manage_edit-campus_sortable_columns',    'cpt_columns_sortable');
add_filter('manage_edit-professor_sortable_columns', 'cpt_columns_sortable');

Apart from that, you also need to correct the $columns format you used, where the correct one is $columns['<column name/slug>'] = '<orderby value passed to WP_Query>', and not $columns['<column name/slug>'] = '<column title>'.

Therefore in your cpt_columns_sortable() function:

// Use this:
$columns['last_modified'] = 'modified';    // 'modified' is a synonym for post_modified
$columns['post_thumbs']   = 'post_thumbs'; // post_thumbs is a custom orderby value

/* Not this:
$columns['last_modified'] = 'Last Modified';
$columns['post_thumbs'] = 'Image';
*/

And that should work, for making the posts sortable by the modification date, because modified is a standard orderby value for WP_Query.

But as for sorting the posts by the post thumbnails (?), you'll need to handle that on your own, e.g. using the pre_get_posts hook.

And note that the_post_thumbnail() echo the output, so don't do "echo the_post_thumbnail".

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.