POST类型的列存储在数据库中的什么位置?

时间:2017-01-26 作者:gradosevic

我想从所有帖子类型中获取所有列,如下所示:

$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;
}
如何实际获取特定帖子类型的帖子类型列,而不考虑页面上下文?数据库中实际存储的列在哪里?这可以通过自定义WP查询完成吗?

如果您转到WordPress中列出某个帖子类型的所有帖子的任何页面(例如edit.php),您可以看到屏幕选项中列出的所有列。

2 个回复
SO网友:prosti

如何实际获取特定帖子类型的帖子类型列

所有帖子类型都应在wp_posts 如果未使用自定义前缀,请在数据库中创建表。

此表具有以下结构。

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]   
如果您需要有关系统上帖子类型的更多信息,请参阅全局变量$wp_post_types.

此变量是一个数组WP_Post_Type Objects、 第一个是这样的:

[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
    )
希望这有帮助。

SO网友:gradosevic

我真的不喜欢回答我自己的问题,但这里有一个对我有用的解决方案,它可以帮助别人。

这是在插件中制作的(您可以将其粘贴在Hello Dolly的末尾进行测试)

//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\' );
输出示例:

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
        )

)

相关推荐