get_post_types()WP 2.9.0

Gets data (objects) of registered post types. Not the posts themselves, but the post types data.

You can filter output by multiple criteria.

1 time — -0.00003 sec (speed of light) | 50000 times — 0.24 sec (very fast) | PHP 7.4.8, WP 5.6.2

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                 => page
label                => Pages
labels               => stdClass Object()
description          =>
public               => 1
hierarchical         => 1
exclude_from_search  =>
publicly_queryable   =>
show_ui              => 1
show_in_menu         => 1
show_in_nav_menus    => 1
show_in_admin_bar    => 1
menu_position        => 20
menu_icon            =>
capability_type      => page
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_page
	read_post              => read_page
	delete_post            => delete_page
	edit_posts             => edit_pages
	edit_others_posts      => edit_others_pages
	publish_posts          => publish_pages
	read_private_posts     => read_private_pages
	read                   => read
	delete_posts           => delete_pages
	delete_private_posts   => delete_private_pages
	delete_published_posts => delete_published_pages
	delete_others_posts    => delete_others_pages
	edit_private_posts     => edit_private_pages
	edit_published_posts   => edit_published_pages
	create_posts           => edit_pages
)

rewrite                =>
show_in_rest           => 1
rest_base              => pages
rest_controller_class  => WP_REST_Posts_Controller
  • 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 and or.
Default: 'and'

Examples

0

#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

*/
0

#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
*/
0

#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>';
}
0

#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";
}
0

#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>
0

#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;
}
0

#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() code WP 6.5.2

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 );
}