Only get post types based on support

I am trying to retrieve a list including both builtin and custom post types:

$post_types = get_post_types(array(
  'public' => TRUE,
), 'objects');

The above almost works, but I would like to exclude the attachment from this list, only returning post types with specific support such as editor, title and thumbnail. Is this possible?

3 s
3

I found out that get_post_types_by_support() seems to be the solution to get the desired result:

$post_types = get_post_types_by_support(array('title', 'editor', 'thumbnail'));

The above will return post, page and any custom post type that supports title, editor and thumbnail.

Since this will also return private post types, we could loop through the list and check if the type is viewable at the frontend. This can be done by using the is_post_type_viewable() function:

foreach ($post_types as $key => $post_type) {
  if (!is_post_type_viewable($post_type)) {
    unset($post_types[$post_type]);
  }
}

Leave a Comment