get_post_types( array|string $args = array(), string $output = 'names', string $operator = 'and' ): string[]|WP_Post_Type[]
- Since
- 2.9.0
- Source
wp-includes/post.php:1620
Retrieves the post types currently registered on the site by matching them against an array of key value pairs. The $output argument decides whether you get back plain name strings or full WP_Post_Type objects, and $operator decides whether all, any, or none of the $args conditions must match. Because it reads the live $wp_post_types global, it only sees types registered before the call runs, so it is normally used on or after the init hook.
Compatibility
- WordPress
- since 2.9.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$argsarray|stringoptional- An array of key => value arguments to match against the post type objects. Default empty array.Default:
array() $outputstringoptional- The type of output to return. Either 'names' or 'objects'. Default 'names'.Default:
'names' $operatorstringoptional- The logical operation to perform. 'or' means only one element from the array needs to match; 'and' means all elements must match; 'not' means no elements may match. Default 'and'.Default:
'and'
Return value
string[]|WP_Post_Type[]- An array of post type names or objects.
Code examples
Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.
List the names of all public post types
A quick way to see which post types a theme or plugin can safely show in a public archive or search form.
$public_post_types = get_post_types( array( 'public' => true ) );
echo '<pre>' . esc_html( print_r( $public_post_types, true ) ) . '</pre>';On a fresh install this includes 'post', 'page' and 'attachment' since all three are registered as public.
Get full post type objects to read their labels
Sometimes a name string is not enough and you need the labels or capabilities that only live on the object.
$post_type_objects = get_post_types( array( 'public' => true ), 'objects' );
foreach ( $post_type_objects as $post_type ) {
printf(
'<p>%s → %s</p>',
esc_html( $post_type->name ),
esc_html( $post_type->labels->name )
);
}Common problems and fixes · 3
- Why does get_post_types() return an empty array or miss my custom post type?
- Why am I getting an array of strings when I expected WP_Post_Type objects?
- Why does adding a second key to $args exclude a post type I expected to see?
Why does get_post_types() return an empty array or miss my custom post type?
Why am I getting an array of strings when I expected WP_Post_Type objects?
Why does adding a second key to $args exclude a post type I expected to see?
Alternatives and related functions
get_post_type_object- When you already know the exact post type name and just need its single WP_Post_Type object instead of filtering a whole list.
post_type_exists- When all you need is a true or false check for whether a given post type name is registered.
register_post_type- When the post type is not registered yet and needs to be added to $wp_post_types before get_post_types() can see it.
Performance profile
How much work a call to get_post_types() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 15–16
- Plugin surface
- None
- Called by
- 50
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 17.
Nothing here hands control to plugin code.
50 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_post_types() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 15–16 | wp_filter_object_list() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 17 instructions, 15–16 executed per call, 1 branch. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 1
- wp_filter_object_list()Filters a list of objects, based on a set of key => value arguments.
Used by · 50
- WP::parse_request()Parses the request to find the correct WordPress query.
- WP_Customize_Nav_Menus::available_item_types()Returns an array of all the available item types.
- WP_Customize_Nav_Menus::search_available_items_query()Performs post queries for available-item searching.
- WP_Embed::cache_oembed()Triggers a caching of all oEmbed results.
- WP_Query::generate_cache_key()Generates cache key.
- WP_Query::get_posts()Retrieves an array of posts based on query variables.
- WP_REST_Block_Pattern_Categories_Controller::get_items_permissions_check()Checks whether a given request has permission to read block patterns.
- WP_REST_Block_Patterns_Controller::get_items_permissions_check()Checks whether a given request has permission to read block patterns.
- WP_REST_Block_Types_Controller::check_read_permission()Checks whether a given block type should be visible.
- WP_REST_Global_Styles_Controller::get_theme_item_permissions_check()Checks if a given request has access to read a single theme global styles config.
- WP_REST_Menu_Items_Controller::check_has_read_only_access()Checks whether the current user has read permission for the endpoint.
- WP_REST_Menus_Controller::check_has_read_only_access()Checks whether the current user has read permission for the endpoint.
Show all 50
- WP_REST_Pattern_Directory_Controller::get_items_permissions_check()Checks whether a given request has permission to view the local block pattern directory.
- WP_REST_Post_Search_Handler::__construct()Constructor.
- WP_REST_Post_Statuses_Controller::check_read_permission()Checks whether a given post status should be visible.
- WP_REST_Post_Statuses_Controller::get_items_permissions_check()Checks whether a given request has permission to read post statuses.
- WP_REST_Post_Types_Controller::get_items()Retrieves all public post types.
- WP_REST_Post_Types_Controller::get_items_permissions_check()Checks whether a given request has permission to read types.
- WP_REST_Server::add_active_theme_link_to_index()Adds a link to the active theme for users who have proper permissions.
- WP_REST_Templates_Controller::get_item_permissions_check()Checks if a given request has access to read a single template.
- WP_REST_Templates_Controller::get_items_permissions_check()Checks if a given request has access to read templates.
- WP_REST_Themes_Controller::check_read_active_theme_permission()Checks if a theme can be read.
- WP_REST_URL_Details_Controller::permissions_check()Checks whether a given request has permission to read remote URLs.
- WP_REST_Users_Controller::get_collection_params()Retrieves the query params for collections.
- WP_REST_Users_Controller::get_item_permissions_check()Checks if a given request has access to read a user.
- WP_REST_Users_Controller::get_items()Retrieves all users.
- WP_REST_Users_Controller::get_items_permissions_check()Permissions check for getting all users.
- WP_Rewrite::generate_rewrite_rules()Generates rewrite rules from a permalink structure.
- WP_Screen::render_view_mode()Renders the list table view mode preferences.
- WP_Sitemaps_Posts::get_object_subtypes()Returns the public post types, which excludes nav_items and similar types.
- WP_Sitemaps_Users::get_users_query_args()Returns the query args for retrieving users to list in the sitemap.
- WP_Terms_List_Table::__construct()Constructor.
- WP_Theme::get_post_templates()Returns the theme's post templates.
- WP_User_Query::prepare_query()Prepares the query variables.
- _WP_Editors::wp_link_query()Performs post queries for internal linking.
- _add_post_type_submenus()Adds submenus for post types.
- _build_block_template_result_from_post()Builds a unified template object based a post Object.
- _get_last_post_time()Gets the timestamp of the last time any post was modified or published.
- block_core_navigation_link_build_variations()Returns an array of variations for the navigation link block.
- create_initial_rest_routes()Registers default REST API routes.
- do_all_enclosures()Performs all enclosures.
- do_all_pingbacks()Performs all pingbacks.
- do_all_trackbacks()Performs all trackbacks.
- export_wp()Generates the WXR export file for download.
- get_pages()Retrieves an array of pages (or hierarchical post type items).
- get_permalink()Retrieves the full permalink for the current post or post ID.
- get_post_embed_url()Retrieves the URL to embed a specific post in an iframe.
- get_template_hierarchy()Gets the template hierarchy for the given template slug to be created.
- redirect_guess_404_permalink()Attempts to guess the correct URL for a 404 request based on query vars.
- register_block_core_footnotes_post_meta()Registers the footnotes meta field required for footnotes to work.
Source code
function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) { global $wp_post_types; $field = ( 'names' === $output ) ? 'name' : false; return wp_filter_object_list( $wp_post_types, $args, $operator, $field );}Changelog
Introduced in 2.9.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, from
src/wp-includes/post.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.