Please add this code block for sorting CPTs
-
Hello,
I have a feature request, one that was requested some years ago but never implemented.
Please consider adding the code block below to the ‘Show modified Date in admin lists’ plugin. This will allow custom post types to be sorted by modification date. This is a need my company has, as with anyone needing this feature for pages and posts. There’s no downside that I know of in providing this feature. I have tested this locally.
Thanks for your consideration.
/**
* Registers the 'Modified Date' column as sortable for all public custom post types.
*
* Runs on init at priority 20 to ensure all custom post types have been registered
* (typically at priority 10) before iterating them. The built-in post/page/media
* cases are handled by the static filters above; this covers any non-built-in CPT.
* Each CPT gets its own dynamic filter: manage_edit-{post_type}_sortable_columns.
*/
function modified_column_register_sortable_for_cpts() {
$custom_post_types = get_post_types( array( 'public' => true, '_builtin' => false ), 'names' );
foreach ( $custom_post_types as $post_type ) {
add_filter( "manage_edit-{$post_type}_sortable_columns", 'modified_column_register_sortable' );
}
}
// Run after CPTs are registered (priority 20) so get_post_types() returns them all.
add_action( 'init', 'modified_column_register_sortable_for_cpts', 20 );
You must be logged in to reply to this topic.