Get a list of all custom post type namesslugs

admin2025-06-05  3

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.

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.

Share Improve this question edited Dec 23, 2018 at 12:45 at least three characters asked Dec 23, 2018 at 11:22 at least three charactersat least three characters 33712 silver badges28 bronze badges 15
  • How do you use that function? Show your current code, pls. – Krzysiek Dróżdż Commented Dec 23, 2018 at 11:44
  • Just 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
  • But where do you use it? How do you use it? – Krzysiek Dróżdż Commented Dec 23, 2018 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 Commented Dec 23, 2018 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ż Commented Dec 23, 2018 at 12:20
 |  Show 10 more comments

1 Answer 1

Reset to default 2

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.
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749073361a316124.html

最新回复(0)