I'm trying to create a dropdown-list of all my custom post types.

It would look like this:

  <option value="post">Post</option>
  <option value="book">Book</option>
  <option value="some-other-post-type">Something</option>
  <option value="team">Team Members</option>

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


It returns an empty array... no custom post types that I registered.

  • How do you use that function? Show your current code, pls. – Krzysiek Dróżdż Dec 23 '18 at 11:44
  • Just get_post_types() and it returns an object but no custom post types included. – at least three characters Dec 23 '18 at 12:12
  • But where do you use it? How do you use it? – Krzysiek Dróżdż Dec 23 '18 at 12:12
  • I used it in functions.php. I am just testing it to see what it returns. I'm trying to find a starting point to build the actual function. i'm not sure where to start to get the post type names – at least three characters Dec 23 '18 at 12:17
  • it all depends on context... Seriously, show us your code, or we won’t be able to help you... It’s hard to guess what’s wrong with code that we can’t see... – Krzysiek Dróżdż Dec 23 '18 at 12:20

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:

  • You can't get these post types too soon. It's very common practice to register the post types on init hook, so you won't get these post types before that hook is fired up.
  • If you'll query for public post types, you'll get only the post types that are registered as public (and not that are public based on other args)...
  • By default this function will return you only names of post types. If you want to obtain more info, you can pass objects as second param.
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.