2

I'm trying to add a column to the trash page for posts and pages but can't find a way to hook into that specific area. I've been able to use functions like manage_posts_columns and manage_pages_columns to add columns, but these hooks add columns to more than just the trash view.

I can see from the URL that the trash has post_status=trash but haven't found a way to hook into that. The generic manage_{$post_type}_posts_columns doesn't seem to fit since I need a post_status, not a post_type.

I've also looked at get_current_screen but it doesn't return a post_status. Am I missing a function or obvious way to do this?

2

You can check the value of the post_status query variable and make sure that it's set to trash:

function wpse239286_trash_column( $columns ) {

    // Bail if we're not looking at trash.
    $status = get_query_var( 'post_status' );
    if ( 'trash' !== $status ) {
        return $columns;
    }

    return array_merge( $columns, 
        array( 'trash_column' => __( 'Trash Column', 'text-domain' ) )
    );
}
add_filter( 'manage_posts_columns' , 'wpse239286_trash_column' );
add_filter( 'manage_pages_columns' , 'wpse239286_trash_column' );
  • Cool, thanks. Having to use $_GET['post_status'] feels kind of kludgy, but it looks like there's no hook available otherwise. – j08691 Sep 20 '16 at 13:37
  • I'm with you on that. I dressed it up slightly, using get_query_var( 'post_status' ) instead of checking $_GET['post_status'] directly, but I didn't see a hook either. – Dave Romsey Sep 20 '16 at 20:32

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.