How to Remove Certain Screen Options and Table Columns from post type in wp_list_table?

The question:

On the page where we list all posts made: example.domain.com/wp-admin/edit.php

There is a Screen Options tab in the upper right corner. Upon clicking this, a menu slides down. Contained within this slide down menu, are check boxes. When checking or unchecking these check boxes, certain columns in the post table are shown/hidden, depending on whether they are checked or not.

I am looking for a way to programmatically obtain a list of all of these registered Screen Options for this post type (Default post type: post). Once I obtain a list of these registered screen options, I will check to see if a certain array of options are listed. If they match a listed item, I plan on removing those options.

How to Remove Certain Screen Options and Table Columns from post type in wp_list_table?
Click Here For Full Size Screenshot

Question
How can I programmatically obtain a list of all registered Screen Options on post types (page with table listing each post)?

Looking for a solution similar to the solution provided for removing certain screen options and meta boxes from add/edit post type.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

What you need is to modify the $columns variable that is used during list display which you can modify using the 'manage_posts_columns' and 'manage_pages_columns' hooks for post_type='post' and post_type='page', respectively. If you want to ignore custom post types you can inspect the 2nd parameter passed to 'manage_posts_columns' as I did in my example to show how.

So let’s assume you want to get rid of the “Comments” screen option and it’s associated column which you see in these screenshot for Posts and Pages, respectively:

How to Remove Certain Screen Options and Table Columns from post type in wp_list_table?

Drop the following class into your theme’s functions.php file or in a plugin you might be building and this code will remove the “Comments” screen option and it’s associated column (Since this answer is similar to your other question, I added a "2" to the class name, hence in Michael_Ecklunds_Admin_Customizer2):

class Michael_Ecklunds_Admin_Customizer2 {
  function __construct() {
    add_action( 'manage_pages_columns', array( $this, 'manage_columns' ) );
    add_action( 'manage_posts_columns', array( $this, 'manage_columns' ), 10, 2 );
  }
  function manage_columns( $columns, $post_type = 'page' ) { //2nd param not passed for pages
    if ( in_array( $post_type, array( 'post', 'page' ) ) ) {
      // This is only for posts and pages, 'if (in_array(...))' just illustrates how.
      unset( $columns['comments'] );
    }
    return $columns;
  }
}
new Michael_Ecklunds_Admin_Customizer2();

And here’s what it looks like after you’ve added the above code to a WordPress 3.4 site:

How to Remove Certain Screen Options and Table Columns from post type in wp_list_table?

Using the Zend debugger within PhpStorm here is the inspection of $columns within the 'manage_posts_columns' hook so you can see what values a default installation of WordPress 3.4 has for the Post edit list (I’ve circled the array indexes I referenced in my example, i.e. $columns['comments']:

Hopefully this is what you were looking for?


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment