The get_post_types() WordPress PHP function retrieves a list of all registered post type objects.
Usage
get_post_types($args, $output, $operator);
Custom example:
$args = array('public' => true, '_builtin' => false);
$output = 'names';
$operator = 'and';
$post_types = get_post_types($args, $output, $operator);
Parameters
$args(array|string) Optional: An array of key => value arguments to match against the post type objects. Default:array()$output(string) Optional: The type of output to return. Accepts post type ‘names’ or ‘objects’. Default: ‘names’$operator(string) Optional: 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’
More information
See WordPress Developer Resources: get_post_types
Examples
Retrieve custom public post types
This example retrieves only custom public post types and outputs them as a list.
$args = array('public' => true, '_builtin' => false);
$output = 'names';
$operator = 'and';
$post_types = get_post_types($args, $output, $operator);
if ($post_types) {
echo '<ul>';
foreach ($post_types as $post_type) {
echo '<li>' . $post_type . '</li>';
}
echo '</ul>';
}
Retrieve a named post type as an object
This example retrieves the ‘movies’ post type as an object and displays its name, singular name, and menu icon URL.
$args = array('name' => 'movies');
$post_types = get_post_types($args, 'objects');
foreach ($post_types as $post_type) {
echo '<p>Custom Post Type name: ' . $post_type->name . "<br />";
echo 'Single name: ' . $post_type->labels->singular_name . "<br />";
echo 'Menu icon URL: ' . $post_type->menu_icon . "</p>";
}
Get post types array with name => singular name
This example creates a function that returns an array of post types with name => singular name.
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;
}
Display the HTML dropdown list of post types
This example displays an HTML dropdown list of post types, including custom post types.
$post_types = get_post_types(array('public' => true), 'names', 'and');
?>
<select name="post_type">
<?php foreach ($post_types as $post_type) { ?>
<option value="<?php echo esc_attr($post_type); ?>"><?php echo esc_html($post_type); ?></option>
<?php } ?>
</select>