0

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

1 Answer 1

1

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".

3
  • Sally CJ, thanks for clearing that up for me. I didn't realize to add separate filters for each cpt and removing the echo with the_post_thumbnail(), silly me. Is not the post_thumbs column sort the posts by the post thumbnails anyway in the table by the manage sortable filter hook?
    – Krys
    Mar 8, 2022 at 14:32
  • post_thumbs (or any other custom orderby values) does make the posts sortable by that value, i.e. you'd get orderby=post_thumbs in the URL, but to actually sort the posts by that, you need to write your own code.
    – Sally CJ
    Mar 8, 2022 at 14:45
  • 1
    Okay, thanks Sally for your help.
    – Krys
    Mar 8, 2022 at 14:45

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.