I'm trying to create a dropdown-list of all my custom post types.
It would look like this:
<select>
<option value="post">Post</option>
<option value="book">Book</option>
<option value="some-other-post-type">Something</option>
<option value="team">Team Members</option>
</select>
I have come across the get_post_types()
function which is supposed to get a list of all registered post type objects. But it doesn't show my custom post types...
Is it possible to get an array with the slug and the title of all the post types registered in a theme? Considering the number of post types is unknown and dynamic. Every time a new custom post type is added or removed, it should reflect in the dropdown list.
My test:
$args = array(
'public' => true,
'_builtin' => false // Use false to return only custom post types
);
$post_types = get_post_types( $args );
print_r($post_types);
It returns an empty array... no custom post types that I registered.
This question already has answers here: Get list of registered custom post types (5 answers) Closed 6 years ago.I'm trying to create a dropdown-list of all my custom post types.
It would look like this:
<select>
<option value="post">Post</option>
<option value="book">Book</option>
<option value="some-other-post-type">Something</option>
<option value="team">Team Members</option>
</select>
I have come across the get_post_types()
function which is supposed to get a list of all registered post type objects. But it doesn't show my custom post types...
Is it possible to get an array with the slug and the title of all the post types registered in a theme? Considering the number of post types is unknown and dynamic. Every time a new custom post type is added or removed, it should reflect in the dropdown list.
My test:
$args = array(
'public' => true,
'_builtin' => false // Use false to return only custom post types
);
$post_types = get_post_types( $args );
print_r($post_types);
It returns an empty array... no custom post types that I registered.
OK, so the function get_post_types
is exactly what you're looking for.
$args = array(
'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>';
}
But there are few things you should be careful about:
init
hook, so you won't get these post types before that hook is fired up. objects
as second param.
get_post_types()
and it returns an object but no custom post types included. – at least three characters Commented Dec 23, 2018 at 12:12