customization - How to get all `supports` attributes by post type?

admin2025-06-05  2

i.e. when post is registered, like this:

$args= [
  'supports' => ['thumbnail', 'title', 'post-formats'  ...]
]

If later, I want to get all supports attribute for specific post type, which function should I use? i.e. something like get_supports('post');

i.e. when post is registered, like this:

$args= [
  'supports' => ['thumbnail', 'title', 'post-formats'  ...]
]

If later, I want to get all supports attribute for specific post type, which function should I use? i.e. something like get_supports('post');

Share Improve this question asked Dec 15, 2018 at 9:32 T.ToduaT.Todua 5,8909 gold badges52 silver badges81 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 4

There exists the get_all_post_type_supports() to get the supported features for a given post type. It's a wrapper for the _wp_post_type_features global variable:

/**
 * Get all the post type features
 *
 * @since 3.4.0
 *
 * @global array $_wp_post_type_features
 *
 * @param string $post_type The post type.
 * @return array Post type supports list.
 */
function get_all_post_type_supports( $post_type ) {
        global $_wp_post_type_features;

        if ( isset( $_wp_post_type_features[$post_type] ) )
                return $_wp_post_type_features[$post_type];

        return array();
}

Example:

Here's an usage example from the wp shell for the 'post' post type:

wp> print_r( get_all_post_type_supports( 'post' ) );
Array
(
    [title] => 1
    [editor] => 1
    [author] => 1
    [thumbnail] => 1
    [excerpt] => 1
    [trackbacks] => 1
    [custom-fields] => 1
    [comments] => 1
    [revisions] => 1
    [post-formats] => 1
)

Another useful wrapper is get_post_types_by_support().

I've chosen to use $GLOBALS['_wp_post_type_features'], that returns like this:

Array
(
    [post] => Array
        (
            [title] => 1
            [editor] => 1
            [author] => 1
            [thumbnail] => 1
            [excerpt] => 1
            [trackbacks] => 1
            [custom-fields] => 1
            [comments] => 1
            [revisions] => 1
            [post-formats] => 1 
        )

    [page] => Array
        (
            [title] => 1
            [editor] => 1
            [author] => 1
            [thumbnail] => 1
            [page-attributes] => 1
            [custom-fields] => 1
            [comments] => 1
            [revisions] => 1
        )
 ...
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749096704a316334.html

最新回复(0)