get_post_type_object()
概述
get_post_type_object() 函数用于通过名称检索已注册的文章类型对象。它返回一个 WP_Post_Type 对象,包含该文章类型的配置信息,如标签、权限、重写规则等,常用于主题或插件开发中动态获取文章类型属性。
关键要点
- 函数接受一个字符串参数 $post_type,表示已注册的文章类型名称。
- 返回 WP_Post_Type 对象(如果存在)或 null(如果文章类型未注册或参数无效)。
- 内部通过全局变量 $wp_post_types 访问文章类型数组,确保高效检索。
- 与 register_post_type() 相关,用于获取注册时定义的配置。
- 在 WordPress 4.6.0 版本后,返回的对象是 WP_Post_Type 实例,增强了类型安全性。
代码示例
$obj = get_post_type_object( 'post' );
echo $obj->labels->singular_name;注意事项
- 确保传入的文章类型名称已通过 register_post_type() 注册,否则返回 null。
- 对象属性名称可能与 register_post_type() 的参数略有不同,使用时需参考文档。
- 函数不处理未标量或空值,直接返回 null,建议在调用前进行参数验证。
Retrieves a post type object by name.
Description
See also
Parameters
$post_typestringrequired-
The name of a registered post type.
Return
WP_Post_Type|null WP_Post_Type object if it exists, null otherwise.
Source
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 ];
}
Related
| Used by | Description |
|---|---|
is_post_embeddable()wp-includes/post.php |
Determines whether a post is embeddable. |
WP_REST_Font_Faces_Controller::get_items_permissions_check()wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php |
Checks if a given request has access to font faces. |
WP_REST_Font_Families_Controller::get_items_permissions_check()wp-includes/rest-api/endpoints/class-wp-rest-font-families-controller.php |
Checks if a given request has access to font families. |
WP_REST_Template_Autosaves_Controller::__construct()wp-includes/rest-api/endpoints/class-wp-rest-template-autosaves-controller.php |
Constructor. |
WP_REST_Template_Revisions_Controller::__construct()wp-includes/rest-api/endpoints/class-wp-rest-template-revisions-controller.php |
Constructor. |
WP_REST_Navigation_Fallback_Controller::get_item_permissions_check()wp-includes/rest-api/endpoints/class-wp-rest-navigation-fallback-controller.php |
Checks if a given request has access to read fallbacks. |
WP_REST_Global_Styles_Revisions_Controller::__construct()wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php |
Constructor. |
_wp_build_title_and_description_for_single_post_type_block_template()wp-includes/block-template-utils.php |
Builds the title and description of a post-specific template based on the underlying referenced post. |
WP_REST_Menu_Items_Controller::prepare_item_for_database()wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php |
Prepares a single nav menu item for create or update. |
WP_REST_Global_Styles_Controller::get_available_actions()wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php |
Get the link relations available for the post and current user. |
rest_get_route_for_post_type_items()wp-includes/rest-api.php |
Gets the REST API route for a post type. |
WP_REST_Templates_Controller::get_available_actions()wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php |
Get the link relations available for the post and current user. |
WP_REST_Templates_Controller::__construct()wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php |
Constructor. |
wp_force_plain_post_permalink()wp-includes/link-template.php |
Determine whether post should always use a plain permalink structure. |
WP_REST_Autosaves_Controller::__construct()wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php |
Constructor. |
use_block_editor_for_post_type()wp-includes/post.php |
Returns whether a post type is compatible with the block editor. |
register_and_do_post_meta_boxes()wp-admin/includes/meta-boxes.php |
Registers the default post meta boxes, and runs the |
WP_REST_Posts_Controller::get_available_actions()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Gets the link relations available for the post and current user. |
WP_Customize_Manager::check_changeset_lock_with_heartbeat()wp-includes/class-wp-customize-manager.php |
Checks locked changeset with heartbeat API. |
WP_Customize_Manager::handle_override_changeset_lock_request()wp-includes/class-wp-customize-manager.php |
Removes changeset lock when take over request is sent via Ajax. |
WP_Customize_Manager::handle_dismiss_autosave_or_lock_request()wp-includes/class-wp-customize-manager.php |
Deletes a given auto-draft changeset or the autosave revision for a given changeset or delete changeset lock. |
WP_Customize_Manager::handle_changeset_trash_request()wp-includes/class-wp-customize-manager.php |
Handles request to trash a changeset. |
WP_Customize_Manager::grant_edit_post_capability_for_changeset()wp-includes/class-wp-customize-manager.php |
Re-maps ‘edit_post’ meta cap for a customize_changeset post to be the same as ‘customize’ maps. |
get_the_post_type_description()wp-includes/general-template.php |
Retrieves the description for a post type archive. |
wp_load_press_this()wp-admin/press-this.php |
|
WP_REST_Revisions_Controller::__construct()wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php |
Constructor. |
WP_REST_Posts_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Prepares links for the request. |
WP_REST_Posts_Controller::get_item_schema()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Retrieves the post’s schema, conforming to JSON Schema. |
WP_REST_Posts_Controller::get_collection_params()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Retrieves the query params for the posts collection. |
WP_REST_Posts_Controller::sanitize_post_statuses()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Sanitizes and validates the list of post statuses, including whether the user can query private statuses. |
WP_REST_Posts_Controller::check_is_post_type_allowed()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Checks if a given post type can be viewed or managed. |
WP_REST_Posts_Controller::check_read_permission()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Checks if a post can be read. |
WP_REST_Posts_Controller::check_update_permission()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Checks if a post can be edited. |
WP_REST_Posts_Controller::check_create_permission()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Checks if a post can be created. |
WP_REST_Posts_Controller::check_delete_permission()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Checks if a post can be deleted. |
WP_REST_Posts_Controller::prepare_item_for_response()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Prepares a single post output for response. |
WP_REST_Posts_Controller::prepare_item_for_database()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Prepares a single post for create or update. |
WP_REST_Posts_Controller::update_item_permissions_check()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Checks if a given request has access to update a post. |
WP_REST_Posts_Controller::get_item()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Retrieves a single post. |
WP_REST_Posts_Controller::create_item_permissions_check()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Checks if a given request has access to create a post. |
WP_REST_Posts_Controller::__construct()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Constructor. |
WP_REST_Posts_Controller::get_items_permissions_check()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Checks if a given request has access to read posts. |
WP_REST_Post_Types_Controller::get_item()wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php |
Retrieves a specific post type. |
WP_REST_Comments_Controller::check_read_post_permission()wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php |
Checks if the post can be read. |
WP_Customize_Nav_Menus::print_post_type_container()wp-includes/class-wp-customize-nav-menus.php |
Prints the markup for new menu items. |
WP_Customize_Nav_Menus::sanitize_nav_menus_created_posts()wp-includes/class-wp-customize-nav-menus.php |
Sanitizes post IDs for posts created for nav menu items to be published. |
WP_Customize_Nav_Menus::ajax_insert_auto_draft_post()wp-includes/class-wp-customize-nav-menus.php |
Ajax handler for adding a new auto-draft post. |
WP_Customize_Nav_Menu_Item_Setting::get_original_title()wp-includes/customize/class-wp-customize-nav-menu-item-setting.php |
Get original title. |
WP_Customize_Nav_Menu_Item_Setting::get_type_label()wp-includes/customize/class-wp-customize-nav-menu-item-setting.php |
Get type label. |
unregister_post_type()wp-includes/post.php |
Unregisters a post type. |
WP_Customize_Manager::customize_pane_settings()wp-includes/class-wp-customize-manager.php |
Prints JavaScript settings for parent window. |
get_preview_post_link()wp-includes/link-template.php |
Retrieves the URL used for the post preview. |
is_post_type_viewable()wp-includes/post.php |
Determines whether a post type is considered “viewable”. |
wp_admin_bar_customize_menu()wp-includes/admin-bar.php |
Adds the “Customize” link to the Toolbar. |
wp_xmlrpc_server::_toggle_sticky()wp-includes/class-wp-xmlrpc-server.php |
Encapsulates the logic for sticking a post and determining if the user has permission to do so. |
WP_Customize_Nav_Menus::enqueue_scripts()wp-includes/class-wp-customize-nav-menus.php |
Enqueues scripts and styles for Customizer pane. |
WP_Customize_Nav_Menus::load_available_items_query()wp-includes/class-wp-customize-nav-menus.php |
Performs the post_type and taxonomy queries for loading available menu items. |
WP_Posts_List_Table::handle_row_actions()wp-admin/includes/class-wp-posts-list-table.php |
Generates and displays row action links. |
WP_Posts_List_Table::column_title()wp-admin/includes/class-wp-posts-list-table.php |
Handles the title column output. |
WP_Media_List_Table::column_parent()wp-admin/includes/class-wp-media-list-table.php |
Handles the parent column output. |
wp_edit_attachments_query_vars()wp-admin/includes/post.php |
Returns the query variables for the current attachments request. |
export_wp()wp-admin/includes/export.php |
Generates the WXR export file for download. |
get_editable_user_ids()wp-admin/includes/deprecated.php |
Gets the IDs of any users who can edit posts. |
WP_List_Table::months_dropdown()wp-admin/includes/class-wp-list-table.php |
Displays a dropdown for filtering items in the list table by month. |
wp_dashboard_right_now()wp-admin/includes/dashboard.php |
Dashboard widget that displays some basic stats about the site. |
wp_dashboard_setup()wp-admin/includes/dashboard.php |
Registers dashboard widgets. |
get_inline_data()wp-admin/includes/template.php |
Adds hidden fields with the data for use in the inline editor for posts and pages. |
get_media_item()wp-admin/includes/media.php |
Retrieves HTML form for modifying the image attachment. |
_wp_post_thumbnail_html()wp-admin/includes/post.php |
Returns HTML for the post thumbnail meta box. |
_admin_notice_post_locked()wp-admin/includes/post.php |
Outputs the HTML for the notice to say that someone else is editing or has taken over editing of this post. |
wp_write_post()wp-admin/includes/post.php |
Creates a new post from the “Write Post” form using |
get_sample_permalink()wp-admin/includes/post.php |
Returns a sample permalink based on the post name. |
_wp_translate_postdata()wp-admin/includes/post.php |
Renames |
edit_post()wp-admin/includes/post.php |
Updates an existing post with values provided in |
bulk_edit_posts()wp-admin/includes/post.php |
Processes the post data for the bulk editing of posts. |
wp_ajax_query_attachments()wp-admin/includes/ajax-actions.php |
Handles querying attachments via AJAX. |
wp_ajax_add_menu_item()wp-admin/includes/ajax-actions.php |
Handles adding a menu item via AJAX. |
post_author_meta_box()wp-admin/includes/meta-boxes.php |
Displays form field with list of authors. |
post_submit_meta_box()wp-admin/includes/meta-boxes.php |
Displays post submit form fields. |
WP_Comments_List_Table::column_response()wp-admin/includes/class-wp-comments-list-table.php |
|
WP_Terms_List_Table::column_posts()wp-admin/includes/class-wp-terms-list-table.php |
|
Walker_Nav_Menu_Edit::start_el()wp-admin/includes/class-walker-nav-menu-edit.php |
Start the element output. |
_wp_ajax_menu_quick_search()wp-admin/includes/nav-menu.php |
Prints the appropriate response to a menu quick search. |
wp_nav_menu_item_post_type_meta_box()wp-admin/includes/nav-menu.php |
Displays a meta box for a post type menu item. |
WP_Posts_List_Table::inline_edit()wp-admin/includes/class-wp-posts-list-table.php |
Outputs the hidden row displayed when inline editing |
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 |
Constructor. |
WP_Posts_List_Table::ajax_user_can()wp-admin/includes/class-wp-posts-list-table.php |
|
map_meta_cap()wp-includes/capabilities.php |
Maps a capability to the primitive capabilities required of the given user to satisfy the capability being checked. |
WP_Customize_Manager::save()wp-includes/class-wp-customize-manager.php |
Handles customize_save WP Ajax request to save/update a changeset. |
wp_list_categories()wp-includes/category-template.php |
Displays or retrieves the HTML list of categories. |
feed_links_extra()wp-includes/general-template.php |
Displays the links to the extra feeds such as category feeds. |
wp_get_archives()wp-includes/general-template.php |
Displays archive links based on type and format. |
wp_title()wp-includes/general-template.php |
Displays or retrieves page title for all areas of blog. |
post_type_archive_title()wp-includes/general-template.php |
Displays or retrieves title for a post type archive. |
WP_Query::get_queried_object()wp-includes/class-wp-query.php |
Retrieves the currently queried object. |
WP_Query::is_post_type_archive()wp-includes/class-wp-query.php |
Determines whether the query is for an existing post type archive page. |
WP_Query::get_posts()wp-includes/class-wp-query.php |
Retrieves an array of posts based on query variables. |
WP_Query::parse_query()wp-includes/class-wp-query.php |
Parses a query string and sets query type booleans. |
register_taxonomy_for_object_type()wp-includes/taxonomy.php |
Adds an already registered taxonomy to an object type. |
unregister_taxonomy_for_object_type()wp-includes/taxonomy.php |
Removes an already registered taxonomy from an object type. |
wp_get_shortlink()wp-includes/link-template.php |
Returns a shortlink for a post, page, attachment, or site. |
get_adjacent_post()wp-includes/link-template.php |
Retrieves the adjacent post. |
get_post_type_archive_link()wp-includes/link-template.php |
Retrieves the permalink for a post type archive. |
get_post_type_archive_feed_link()wp-includes/link-template.php |
Retrieves the permalink for a post type archive feed. |
get_edit_post_link()wp-includes/link-template.php |
Retrieves the edit post link for post. |
get_delete_post_link()wp-includes/link-template.php |
Retrieves the delete posts link for post. |
get_post_permalink()wp-includes/link-template.php |
Retrieves the permalink for a post of a custom post type. |
wp_admin_bar_my_sites_menu()wp-includes/admin-bar.php |
Adds the “My Sites/[Site Name]” menu and all submenus. |
wp_admin_bar_edit_menu()wp-includes/admin-bar.php |
Provides an edit link for posts and terms. |
get_post_type_archive_template()wp-includes/template.php |
Retrieves path of post type archive template in current or parent template. |
wp_enqueue_media()wp-includes/media.php |
Enqueues all scripts, styles, settings, and templates necessary to use all media JS APIs. |
get_posts_by_author_sql()wp-includes/post.php |
Retrieves the post SQL based on capability, author, and type. |
wp_insert_post()wp-includes/post.php |
Inserts or update a post. |
_count_posts_cache_key()wp-includes/post.php |
Returns the cache key for wp_count_posts() based on the passed arguments. |
wp_count_posts()wp-includes/post.php |
Counts number of posts of a post type and if user has permissions to view. |
_add_post_type_submenus()wp-includes/post.php |
Adds submenus for post types. |
is_post_type_hierarchical()wp-includes/post.php |
Determines whether the post type is hierarchical. |
post_type_exists()wp-includes/post.php |
Determines whether a post type is registered. |
redirect_canonical()wp-includes/canonical.php |
Redirects incoming links to the proper URL based on the site url. |
_update_blog_date_on_post_publish()wp-includes/ms-blogs.php |
Handler for updating the site’s last updated date when a post is published or an already published post is changed. |
_update_blog_date_on_post_delete()wp-includes/ms-blogs.php |
Handler for updating the current site’s last updated date when a published post is deleted. |
wp_setup_nav_menu_item()wp-includes/nav-menu.php |
Decorates a menu item object with the shared navigation menu item properties. |
wp_update_nav_menu_item()wp-includes/nav-menu.php |
Saves the properties of a menu item or create a new one. |
wp_xmlrpc_server::blogger_newPost()wp-includes/class-wp-xmlrpc-server.php |
Creates a new post. |
wp_xmlrpc_server::mw_newPost()wp-includes/class-wp-xmlrpc-server.php |
Creates a new post. |
wp_xmlrpc_server::wp_getPostType()wp-includes/class-wp-xmlrpc-server.php |
Retrieves a post type. |
wp_xmlrpc_server::wp_getComments()wp-includes/class-wp-xmlrpc-server.php |
Retrieves comments. |
wp_xmlrpc_server::wp_getPosts()wp-includes/class-wp-xmlrpc-server.php |
Retrieves posts. |
wp_xmlrpc_server::_insert_post()wp-includes/class-wp-xmlrpc-server.php |
Helper method for wp_newPost() and wp_editPost(), containing shared logic. |
WP_Customize_Control::render_content()wp-includes/class-wp-customize-control.php |
Renders the control’s content. |
Skip to note 3 content
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.Skip to note 4 content
jorge-vitrubio
Examples
Retreive the slug
$current_queried_post_type_slug = get_post_type_object( get_post_type() )->rewrite['slug'];display the slug:
echo $current_queried_post_type_slug;Build the URL for the post_type:
echo home_url( '/'. $current_queried_post_type_slug . '/' );get_post_type_archive_link( get_post_type() )