get_post_types()
Gets data (objects) of registered post types. Not the posts themselves, but the post types data.
You can filter output by multiple criteria.
No Hooks.
Return
String[]|WP_Post_Type[]
. An array of post type names or objects.
Usage
get_post_types( $args, $output, $operator );
- $args(array).
An array of criteria by which the post types will be selected. For the value of each parameter, see the register_post_type() function description.
- name
- label
- singular_label
- description
- public - Logical, if true, then only public post types will be selected (see description register_post_type()).
- publicly_queryable
- exclude_from_search
- show_ui
- capability_type
- edit_cap
- edit_type_cap
- edit_others_cap
- publish_others_cap
- read_cap
- delete_cap
- hierarchical
- supports
- register_meta_box_cb
- taxonomies
- menu_position
- menu_icon
- permalink_epmask
- rewrite
- query_var
-
_builtin
Logical. If true, the built-in WP post types will be returned: page, posts... false - it will return only new post types.Post types related to the criterion
_builtin
:- post
- page
- mediapage
- attachment
- revision
- nav_menu_item — from WP 3.0
- custom post type — from WP 3.0
- _edit_link
Default: presets
- $output(string)
How to output the result. There are 2 possibilities:
names
— an array of names will be returned;objects
— an array of post type data objects will be returned.
Default: `names'
- $operator(string)
- The comparison operator for the specified criteria. Can be:
and
andor
.
Default: 'and'
Examples
#1 Display a list of names of all registered post types
Outputs the registered types of posts:
$post_types = get_post_types(); /* $post_types = Array ( [post] => post [page] => page [attachment] => attachment [revision] => revision [nav_menu_item] => nav_menu_item [article] => article [question] => question ) */
foreach( $post_types as $post_type ) { echo $post_type ."\n"; } /* Will echos: post page attachment revision nav_menu_item article func */
#2 Display a list of post types that have a page in the front
Get all the public types of posts.
$post_types = get_post_types( [ 'publicly_queryable'=>1 ] ); $post_types['page'] = 'page'; // embedded type has no publicly_queryable unset( $post_types['attachment'] ); // remove attachment /* Array [post] => post [custom_type1] => custom_type1 [custom_type2] => custom_type2 [custom_type3] => custom_type3 [page] => page */
#3 Display a list of all public, custom (created) post types
By setting '_builtin'
to false, we exclude the WordPress built-in public post types (post
, page
, attachment
, revision
, nav_menu_item
, custom_css
, customize_changeset
, oembed_cache
, user_request
, wp_block
, wp_template
) and retrieve only registered custom public post types.
$args = [ 'public' => true, '_builtin' => false ]; $output = 'names'; // names or objects, note names is the default $operator = 'and'; // 'and' or 'or' $post_types = get_post_types( $args, $output, $operator ); foreach ( $post_types as $post_type ) { echo '<p>'. $post_type. '</p>'; }
#4 Get the post type by title
Here the output is used in the form of a data object. Output the post type with the name property
:
$args = [ 'name'=>'property' ]; $post_types = get_post_types( $args, 'objects' ); foreach ( $post_types as $post_type ) { echo '<p>Custom Post Type name: ' . $post_type->name . "<br />\n"; echo 'Single name: ' . $post_type->labels->singular_name . "<br />\n"; echo 'Menu icon URL: ' . $post_type->menu_icon . "</p>\n"; }
#5 Display the HTML dropdown list of Post Types.
<?php $args = [ 'public' => true, ]; $post_types = get_post_types( $args, 'objects' ); ?> <select class="widefat" name="post_type"> <?php foreach( $post_types as $post_type_obj ){ $labels = get_post_type_labels( $post_type_obj ); ?> <option value="<?= esc_attr( $post_type_obj->name ) ?>"><?= esc_html( $labels->name ) ?></option> <?php } ?> </select>
#6 Get post types array with name => singular
pairs
You can use this function in a select dropdown option where user can select a post type form existing post types.
function prefix_get_post_types() { $post_types = get_post_types( [], 'objects' ); $posts = array(); foreach ( $post_types as $post_type ) { $posts[ $post_type->name ] = $post_type->labels->singular_name; } return $posts; }
#7 Output a list of the names of all registered post types
This code display all registered post types with no exclusions.
$post_types = get_post_types( '', 'names' ); $lis = []; foreach ( $post_types as $post_type ) { $lis[] = $post_type; } echo "<ul>\n\t<li>". implode( "</li>\n\t<li>", $lis ) ."</li>\n</ul>"; /* Displays: <ul> <li>post</li> <li>page</li> <li>attachment</li> <li>revision</li> <li>nav_menu_item</li> <li>custom_css</li> <li>customize_changeset</li> <li>oembed_cache</li> <li>user_request</li> <li>wp_block</li> <li>wp_template</li> <li>wp_template_part</li> <li>wp_global_styles</li> <li>wp_navigation</li> <li>my_custom</li> </ul> */
Notes
- Global. Array. $wp_post_types List of post types.
- See: register_post_type() for accepted arguments.
Changelog
Since 2.9.0 | Introduced. |
get_post_types() get post types code WP 6.4.1
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 ); }