4

The all view shows all posts including drafts in wp-admin/edit.php. How can I exclude the posts with the draft status in the all view?

7

The show_in_admin_all_list parameter in the register_post_status() function, determines if a given post status is included in the All post table view.

Probably the shortest version is:

add_action( 'init', function() use ( &$wp_post_statuses )
{
    $wp_post_statuses['draft']->show_in_admin_all_list = false;

}, 1 );

but let's avoid modifying the globals directly like this and override the default draft status with:

add_action( 'init', function()
{
    register_post_status( 'draft',
        [
            'label'                     => _x( 'Draft', 'post status' ),
            'protected'                 => true,
            '_builtin'                  => true, 
            'label_count'               => _n_noop( 'Draft <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>' ),
            'show_in_admin_all_list'    => false, // <-- we override this setting
        ]
    );

}, 1 );

where we use the priority 1, since the default draft status is registered at priority 0.

To avoid repeating the default settings and support possible settings changes in the future, we could use the get_post_status_object() instead:

add_action( 'init', function()
{   
    $a = get_object_vars( get_post_status_object( 'draft' ) );
    $a['show_in_admin_all_list'] = false; // <-- we override this setting
    register_post_status( 'draft', $a );

}, 1 );
  • Great, concise answer! One big advantage to this approach is that the post count for all posts is correctly reflected with this code, which is not the case with the answer I posted. – Dave Romsey Feb 6 '17 at 22:53
  • 1
    thanks, I guess there's a workaround to adjust the posts' count for the pre_get_posts approach. @DaveRomsey – birgire Feb 7 '17 at 8:36
  • 1
    I know this is about drafts, but in case someone would like to use this for custom status, be aware there is a bug in WP core core.trac.wordpress.org/ticket/24415 and in some cases, show_in_admin_all_list might be ignored. – Kokers Jan 15 '18 at 17:20
  • Thanks @Kokers for the info and the bug link. Hopefully this will be patched. – birgire Jan 16 '18 at 8:44
3

The code below will remove draft posts from the admin area under the All listing for the post post type.

The query argument all_posts with a value of 1 is added to the menu link to ensure that we're only applying this modification when necessary (The All link under the admin post filters (All, Mine, Published, Sticky, Scheduled, Drafts)) will add this query parameter for us, but this is not the case when clicking admin menu, so we need to add it ourselves.

Place the code below into your theme's functions.php or within a plugin.

// 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'
        );
    }  
}  

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.