0

I want to get all columns from all post types like this:

$postTypes = get_post_types();

$allColumns = array();

foreach($postTypes as $type){
    $allColumns[$type] = get_post_type_columns($type);
}

function get_post_type_columns($type){
   $columns = array();
   //implement
   return $columns;
}

How to actually get post type columns for a specific post type, regardless of the page context? Where are the columns stored in database actually? Can this be done with a custom WP query?

If you go to any page in WordPress that lists all posts for a post type (e.g. edit.php) you can see all columns listed in screen options.

1
  • This is odd! It looks like that the columns are created "on the fly" and not stored in DB! WP_Posts_List_Table->get_columns() method shows several default columns, and for other post types these are probably modified by custom filters. I guess that the only way to get them all is to somehow apply all manage_{$post_type}_posts_columns filters in one place... core.trac.wordpress.org/browser/tags/4.5.3/src/wp-admin/…
    – gradosevic
    Jan 26, 2017 at 20:35

2 Answers 2

1

How to actually get post type columns for a specific post type

All post types should be inside the wp_posts table inside your database, if you haven't used the custom prefix.

This table has the following structure.

Column  Type    Comment
ID  bigint(20) unsigned Auto Increment   
post_author bigint(20) unsigned [0]  
post_date   datetime [0000-00-00 00:00:00]   
post_date_gmt   datetime [0000-00-00 00:00:00]   
post_content    longtext     
post_title  text     
post_excerpt    text     
post_status varchar(20) [publish]    
comment_status  varchar(20) [open]   
ping_status varchar(20) [open]   
post_password   varchar(255) []  
post_name   varchar(200) []  
to_ping text     
pinged  text     
post_modified   datetime [0000-00-00 00:00:00]   
post_modified_gmt   datetime [0000-00-00 00:00:00]   
post_content_filtered   longtext     
post_parent bigint(20) unsigned [0]  
guid    varchar(255) []  
menu_order  int(11) [0]  
post_type   varchar(20) [post]   
post_mime_type  varchar(100) []  
comment_count   bigint(20) [0]   

If you need more info about the posts types you have on your system refer to the global variable $wp_post_types.

This variable is an array holding WP_Post_Type Objects, where the first one is like this:

[post] => WP_Post_Type Object
    (
        [name] => post
        [label] => Posts
        [labels] => stdClass Object
            (
                [name] => Posts
                [singular_name] => Post
                [add_new] => Add New
                [add_new_item] => Add New Post
                [edit_item] => Edit Post
                [new_item] => New Post
                [view_item] => View Post
                [view_items] => View Posts
                [search_items] => Search Posts
                [not_found] => No posts found.
                [not_found_in_trash] => No posts found in Trash.
                [parent_item_colon] => 
                [all_items] => All Posts
                [archives] => Post Archives
                [attributes] => Post Attributes
                [insert_into_item] => Insert into post
                [uploaded_to_this_item] => Uploaded to this post
                [featured_image] => Featured Image
                [set_featured_image] => Set featured image
                [remove_featured_image] => Remove featured image
                [use_featured_image] => Use as featured image
                [filter_items_list] => Filter posts list
                [items_list_navigation] => Posts list navigation
                [items_list] => Posts list
                [menu_name] => Posts
                [name_admin_bar] => Post
            )

        [description] => 
        [public] => 1
        [hierarchical] => 
        [exclude_from_search] => 
        [publicly_queryable] => 1
        [show_ui] => 1
        [show_in_menu] => 1
        [show_in_nav_menus] => 1
        [show_in_admin_bar] => 1
        [menu_position] => 5
        [menu_icon] => 
        [capability_type] => post
        [map_meta_cap] => 1
        [register_meta_box_cb] => 
        [taxonomies] => Array
            (
            )

        [has_archive] => 
        [query_var] => 
        [can_export] => 1
        [delete_with_user] => 1
        [_builtin] => 1
        [_edit_link] => post.php?post=%d
        [cap] => stdClass Object
            (
                [edit_post] => edit_post
                [read_post] => read_post
                [delete_post] => delete_post
                [edit_posts] => edit_posts
                [edit_others_posts] => edit_others_posts
                [publish_posts] => publish_posts
                [read_private_posts] => read_private_posts
                [read] => read
                [delete_posts] => delete_posts
                [delete_private_posts] => delete_private_posts
                [delete_published_posts] => delete_published_posts
                [delete_others_posts] => delete_others_posts
                [edit_private_posts] => edit_private_posts
                [edit_published_posts] => edit_published_posts
                [create_posts] => edit_posts
            )

        [rewrite] => 
        [show_in_rest] => 1
        [rest_base] => posts
        [rest_controller_class] => WP_REST_Posts_Controller
    )

Hope this helps.

1
  • Thanks for the idea. Unfortunately, this object does not contain any data related to columns. It's a mystery where they're stored.
    – gradosevic
    Jan 26, 2017 at 19:56
0

I don't really like answering my own questions, but here is a solution that worked for me, it could help someone.

This is made inside a plugin (you can just paste it at the end of Hello Dolly to test it)

//The first thing is to load WordPress core classes that will be used
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
require_once( ABSPATH . 'wp-admin/includes/class-wp-posts-list-table.php' );

//Create a helper class from existing WordPress core class
class PostTypeColumnsHelper extends WP_Posts_List_Table{
    public function setPostType($type){
        $this->screen->post_type = $type;
    }
}

//create a function to collect the columns
function get_column_names(){
    $postColumns = array();
    $c = new PostTypeColumnsHelper();

    $types = get_post_types();
    foreach($types as $type){
        $c->setPostType($type);
        $postColumns[$type] = $c->get_columns();
    }
    print_r($postColumns);die;
}

//call function for collecting column names, once admin is ready
add_action( 'admin_init', 'get_column_names' );

OUTPUT EXAMPLE:

Array
(
    [post] => Array
        (
            [cb] => <input type="checkbox" />
            [title] => Title
            [author] => Author
            [categories] => Categories
            [tags] => Tags
            [comments] => <span class="vers comment-grey-bubble" title="Comments"><span class="screen-reader-text">Comments</span></span>
            [date] => Date
        )

    [page] => Array
        (
            [cb] => <input type="checkbox" />
            [title] => Title
            [author] => Author
            [comments] => <span class="vers comment-grey-bubble" title="Comments"><span class="screen-reader-text">Comments</span></span>
            [date] => Date
        )

    [attachment] => Array
        (
            [cb] => <input type="checkbox" />
            [title] => Title
            [author] => Author
            [comments] => <span class="vers comment-grey-bubble" title="Comments"><span class="screen-reader-text">Comments</span></span>
            [date] => Date
        )

    [revision] => Array
        (
            [cb] => <input type="checkbox" />
            [title] => Title
            [author] => Author
            [date] => Date
        )

    [nav_menu_item] => Array
        (
            [cb] => <input type="checkbox" />
            [title] => Title
            [date] => Date
        )

    [bp-email] => Array
        (
            [cb] => <input type="checkbox" />
            [title] => Title
            [situation] => Situations
            [date] => Date
        )

)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.