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