$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Only get post types based on support|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Only get post types based on support

matteradmin9PV0评论

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?

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?

Share Improve this question edited Jan 16, 2018 at 6:33 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Jan 15, 2018 at 15:55 CyclonecodeCyclonecode 1,1841 gold badge9 silver badges32 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 10

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

get_post_types() accepts an array of arguments to match the fields of a post type object. So, you could do something like this (not tested):

$post_types = get_post_types(array(
  'public'   => true,
  'supports' => array( 'editor', 'title', 'thumbnail' )
), 'objects');

Unfortunately, you can not set someting like "exclude" in this function, and also you get only post types that support exactly 'editor', 'title', 'thumbnail', no more and no less.

Or you could use get_post_types_by_support() (only for WP 4.5 and greater. Also, note that you can not exclude specific post types with this function either, but for the specific case of support for editor, title, thumbnail, attachment post type will be excluded in most cases).

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

If you want something that will work in any case, I would try to get post types based in a wider criteria, then build your own array, something like this:

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

$post_types = [];

foreach($_post_types as $post_type) {
    // In most cases, attachment post type won't be here, but it can be
    if( $post_type->name !== 'attachment' ) {
        $post_types[] = $post_type;
    }
}

The simplest approach for the OP's question would be to just unset 'attachment' from the returned array;

$post_types = get_post_types(array('public' => TRUE,), 'objects');
unset($post_types['attachment']);

While not as elegant as the other solutions it has the least overhead.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far