Does anyone know of a way to hide draft post so that when "All" post is selected, draft post is not included?
I tried adding this code to my functions.php file but it lead to an error, I'm assuming it's because this code was written for a previous version of WordPress.
Is the code below the correct code I should be using?
// Add a query argument to the Posts admin menu.
// This is used to ensure that we only apply our special filtering when needed.
add_action( 'admin_menu', 'wpse255311_admin_menu', PHP_INT_MAX );
function wpse255311_admin_menu() {
global $menu, $submenu;
$parent = 'edit.php';
foreach( $submenu[ $parent ] as $key => $value ){
if ( $value['2'] === 'edit.php' ) {
$submenu[ $parent ][ $key ]['2'] = 'edit.php?all_posts=1';
break;
}
}
}
// Hide draft posts from All listing in admin.
add_filter( 'pre_get_posts', 'wpse255311_pre_get_posts' );
function wpse255311_pre_get_posts( $wp_query ) {
$screen = get_current_screen();
// Ensure the the all_posts argument is set and == to 1
if ( ! isset( $_GET['all_posts'] ) || $_GET['all_posts'] != 1 ) {
return;
}
// Bail if we're not on the edit-post screen.
if ( 'edit-post' !== $screen->id ) {
return;
}
// Bail if we're not in the admin area.
if ( ! is_admin() ) {
return;
}
// Ensure we're dealing with the main query and the 'post' post type
// Only include certain post statuses.
if ( $wp_query->is_main_query() && $wp_query->query['post_type'] === 'post' ) {
$wp_query->query_vars['post_status'] = array (
'publish',
'private',
'future'
);
}
}