返回介绍

get_post_type_object()

发布于 2017-09-10 23:59:53 字数 16014 浏览 958 评论 0 收藏 0

get_post_type_object( string $post_type )

Retrieves a post type object by name.


description


参数

$post_type

(string) (Required) The name of a registered post type.


返回值

(WP_Post_Type|null) WP_Post_Type object if it exists, null otherwise.


源代码

File: wp-includes/post.php

function get_post_type_object( $post_type ) {
	global $wp_post_types;

	if ( ! is_scalar( $post_type ) || empty( $wp_post_types[ $post_type ] ) ) {
		return null;
	}

	return $wp_post_types[ $post_type ];
}

更新日志

Versiondescription
4.6.0Object returned is now an instance of WP_Post_Type.
3.0.0Introduced.

相关函数

Used By

  • wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php: WP_REST_Revisions_Controller::delete_item_permissions_check()
  • wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php: WP_REST_Revisions_Controller::__construct()
  • wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php: WP_REST_Revisions_Controller::get_items_permissions_check()
  • wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php: WP_REST_Attachments_Controller::create_item_permissions_check()
  • wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php: WP_REST_Terms_Controller::prepare_links()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::prepare_links()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::get_item_schema()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::get_collection_params()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::sanitize_post_statuses()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::check_is_post_type_allowed()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::check_read_permission()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::check_update_permission()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::check_create_permission()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::check_delete_permission()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::prepare_item_for_database()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::update_item_permissions_check()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::get_item()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::create_item_permissions_check()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::__construct()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::get_items_permissions_check()
  • wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php: WP_REST_Post_Types_Controller::get_item()
  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php: WP_REST_Comments_Controller::check_read_post_permission()
  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php: WP_REST_Comments_Controller::prepare_links()
  • wp-includes/class-wp-customize-nav-menus.php: WP_Customize_Nav_Menus::print_post_type_container()
  • wp-includes/class-wp-customize-nav-menus.php: WP_Customize_Nav_Menus::sanitize_nav_menus_created_posts()
  • wp-includes/class-wp-customize-nav-menus.php: WP_Customize_Nav_Menus::ajax_insert_auto_draft_post()
  • wp-includes/customize/class-wp-customize-nav-menu-item-setting.php: WP_Customize_Nav_Menu_Item_Setting::get_original_title()
  • wp-includes/customize/class-wp-customize-nav-menu-item-setting.php: WP_Customize_Nav_Menu_Item_Setting::get_type_label()
  • wp-includes/post.php: unregister_post_type()
  • wp-includes/link-template.php: get_preview_post_link()
  • wp-includes/post.php: is_post_type_viewable()
  • wp-includes/admin-bar.php: wp_admin_bar_customize_menu()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::_toggle_sticky()
  • wp-includes/class-wp-customize-nav-menus.php: WP_Customize_Nav_Menus::enqueue_scripts()
  • wp-includes/class-wp-customize-nav-menus.php: WP_Customize_Nav_Menus::load_available_items_query()
  • wp-admin/includes/class-wp-posts-list-table.php: WP_Posts_List_Table::handle_row_actions()
  • wp-admin/includes/class-wp-posts-list-table.php: WP_Posts_List_Table::column_title()
  • wp-admin/includes/class-wp-media-list-table.php: WP_Media_List_Table::column_parent()
  • wp-admin/includes/post.php: wp_edit_attachments_query_vars()
  • wp-admin/includes/export.php: export_wp()
  • wp-admin/includes/deprecated.php: get_editable_user_ids()
  • wp-admin/includes/dashboard.php: wp_dashboard_right_now()
  • wp-admin/includes/dashboard.php: wp_dashboard_setup()
  • wp-admin/includes/template.php: get_inline_data()
  • wp-admin/includes/media.php: get_media_item()
  • wp-admin/includes/post.php: _wp_post_thumbnail_html()
  • wp-admin/includes/post.php: _admin_notice_post_locked()
  • wp-admin/includes/post.php: wp_write_post()
  • wp-admin/includes/post.php: get_sample_permalink()
  • wp-admin/includes/post.php: _wp_translate_postdata()
  • wp-admin/includes/post.php: edit_post()
  • wp-admin/includes/post.php: bulk_edit_posts()
  • wp-admin/includes/ajax-actions.php: wp_ajax_query_attachments()
  • wp-admin/includes/ajax-actions.php: wp_ajax_add_menu_item()
  • wp-admin/includes/meta-boxes.php: post_submit_meta_box()
  • wp-admin/includes/class-wp-comments-list-table.php: WP_Comments_List_Table::column_response()
  • wp-admin/includes/class-wp-terms-list-table.php: WP_Terms_List_Table::column_posts()
  • wp-admin/includes/class-walker-nav-menu-edit.php: Walker_Nav_Menu_Edit::start_el()
  • wp-admin/includes/nav-menu.php: _wp_ajax_menu_quick_search()
  • wp-admin/includes/nav-menu.php: wp_nav_menu_item_post_type_meta_box()
  • wp-admin/includes/class-wp-posts-list-table.php: WP_Posts_List_Table::inline_edit()
  • wp-admin/includes/class-wp-posts-list-table.php: WP_Posts_List_Table::no_items()
  • wp-admin/includes/class-wp-posts-list-table.php: WP_Posts_List_Table::get_bulk_actions()
  • wp-admin/includes/class-wp-posts-list-table.php: WP_Posts_List_Table::extra_tablenav()
  • wp-admin/includes/class-wp-posts-list-table.php: WP_Posts_List_Table::__construct()
  • wp-admin/includes/class-wp-posts-list-table.php: WP_Posts_List_Table::ajax_user_can()
  • wp-includes/capabilities.php: map_meta_cap()
  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::save()
  • wp-includes/category-template.php: wp_list_categories()
  • wp-includes/general-template.php: feed_links_extra()
  • wp-includes/general-template.php: wp_get_archives()
  • wp-includes/general-template.php: wp_title()
  • wp-includes/general-template.php: post_type_archive_title()
  • wp-includes/class-wp-query.php: WP_Query::get_queried_object()
  • wp-includes/class-wp-query.php: WP_Query::is_post_type_archive()
  • wp-includes/class-wp-query.php: WP_Query::get_posts()
  • wp-includes/class-wp-query.php: WP_Query::parse_query()
  • wp-includes/taxonomy.php: register_taxonomy_for_object_type()
  • wp-includes/taxonomy.php: unregister_taxonomy_for_object_type()
  • wp-includes/link-template.php: wp_get_shortlink()
  • wp-includes/link-template.php: get_adjacent_post()
  • wp-includes/link-template.php: get_post_type_archive_link()
  • wp-includes/link-template.php: get_post_type_archive_feed_link()
  • wp-includes/link-template.php: get_edit_post_link()
  • wp-includes/link-template.php: get_delete_post_link()
  • wp-includes/link-template.php: get_post_permalink()
  • wp-includes/admin-bar.php: wp_admin_bar_my_sites_menu()
  • wp-includes/admin-bar.php: wp_admin_bar_edit_menu()
  • wp-includes/template.php: get_post_type_archive_template()
  • wp-includes/media.php: wp_prepare_attachment_for_js()
  • wp-includes/media.php: wp_enqueue_media()
  • wp-includes/post.php: get_posts_by_author_sql()
  • wp-includes/post.php: _count_posts_cache_key()
  • wp-includes/post.php: wp_count_posts()
  • wp-includes/post.php: _add_post_type_submenus()
  • wp-includes/post.php: is_post_type_hierarchical()
  • wp-includes/post.php: post_type_exists()
  • wp-includes/canonical.php: redirect_canonical()
  • wp-includes/ms-blogs.php: _update_blog_date_on_post_publish()
  • wp-includes/ms-blogs.php: _update_blog_date_on_post_delete()
  • wp-includes/nav-menu.php: wp_setup_nav_menu_item()
  • wp-includes/nav-menu.php: wp_update_nav_menu_item()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::blogger_newPost()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::mw_newPost()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_getPostType()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_getComments()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_getPosts()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::_insert_post()
  • wp-includes/class-wp-customize-control.php: WP_Customize_Control::render_content()
  • Show 104 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Examples

    
    $obj = get_post_type_object( 'post' );
    echo $obj->labels->singular_name;
    

    Technically, this is the same as

    
    global $wp_post_types;
    $obj = $wp_post_types['post'];
    echo $obj->labels->singular_name; 
    

    print_r( $obj ) might return something like this:

    
    stdClass Object
    (
    	[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
    			[search_items] => Search Posts
    			[not_found] => No posts found
    			[not_found_in_trash] => No posts found in Trash
    			[parent_item_colon] => 
    		)
    	[description] => 
    	[publicly_queryable] => 1
    	[exclude_from_search] => 
    	[_builtin] => 1
    	[_edit_link] => post.php?post=%d
    	[capability_type] => post
    	[hierarchical] => 
    	[public] => 1
    	[rewrite] => 
    	[query_var] => 
    	[register_meta_box_cb] => 
    	[taxonomies] => Array
    		(
    		)
    	[show_ui] => 1
    	[menu_position] => 
    	[menu_icon] => 
    	[permalink_epmask] => 1
    	[can_export] => 1
    	[show_in_nav_menus] => 1
    	[name] => post
    	[cap] => stdClass Object
    		(
    			[edit_post] => edit_post
    			[edit_posts] => edit_posts
    			[edit_others_posts] => edit_others_posts
    			[publish_posts] => publish_posts
    			[read_post] => read_post
    			[read_private_posts] => read_private_posts
    			[delete_post] => delete_post
    		)
    	[label] => Posts
    )
    

    Assuming we have the post type ‘certification’, we could do this:

    
    $obj = get_post_type_object( 'certification' );
    

    print_r( $obj ) might return something like this:

    
    stdClass Object
    (
    	[labels] => stdClass Object
    		(
    			[name] => Certification
    			[singular_name] => Certification
    			[add_new] => Add New
    			[add_new_item] => Add New Certification
    			[edit_item] => Edit Certification
    			[new_item] => New Page
    			[view_item] => View Certification
    			[search_items] => Search Certification
    			[not_found] => Not found
    			[not_found_in_trash] => Not found in Trash
    			[parent_item_colon] => Parent Certification:
    			[all_items] => All Certifications
    			[menu_name] => Certifications
    			[update_item] => Update Certification
    			[name_admin_bar] => Certification
    		)
    	[description] => Certifications
    	[public] => 1
    	[hierarchical] => 1
    	[exclude_from_search] => 
    	[publicly_queryable] => 1
    	[show_ui] => 1
    	[show_in_menu] => 
    	[show_in_nav_menus] => 1
    	[show_in_admin_bar] => 1
    	[menu_position] => 5
    	[menu_icon] => dashicons-welcome-widgets-menus
    	[capability_type] => post
    	[map_meta_cap] => 1
    	[register_meta_box_cb] => 
    	[taxonomies] => Array
    		(
    			[0] => objective
    		)
    	[has_archive] => 1
    	[rewrite] => Array
    		(
    			[slug] => certification
    			[with_front] => 1
    			[pages] => 1
    			[feeds] => 1
    			[ep_mask] => 1
    		)
    	[query_var] => certification
    	[can_export] => 1
    	[delete_with_user] => 
    	[_builtin] => 
    	[_edit_link] => post.php?post=%d
    	[label] => Certification
    	[name] => certification
    	[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
    		)
    )
    

    Note that the object’s attribute names are slightly different than the arguments expected for the register_post_type() function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文