Get a list of all custom post type names/slugs [duplicate]

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.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)