add_post_type_support()
Adds support for additional features (functions) for the post type.
For example, it adds meta boxes on the post edit screen: post thumbnail, comments, custom fields, etc.
In addition, the revisions function determines whether the post type will store revisions. The autosave function determines whether the post type will be automatically saved. The comments function determines whether the number of comments will be displayed on the edit screen.
Typically, the supported meta boxes for the post are specified when registering a new post type in the register_post_type( 'supports' => [ 'title', 'editor' ] ) function. This function is needed to add additional features after the post registration on the fly.
The function should be called after the post type has been registered. Because it works with the global variable $_wp_post_type_features, in which the post type must already exist.
It is recommended to use the function on the init action.
No Hooks.
Returns
null. Nothing.
Usage
add_post_type_support( $post_type, $feature, ...$args );
- $post_type(string) (required)
- The post type.
- $feature(string/array) (required)
The feature to add. Possible parameters:
title- title block;editor- content input block;author- author selection block;thumbnail- post thumbnail selection block;excerpt- quote (short description) input block;trackbacks- notifications block;custom-fields- custom fields setup block;comments- comments block;revisions- revisions block;page-attributes- permanent page attributes block (template and hierarchical relationship of posts, hierarchy must be enabled). Can be used instead.post-formats– post formats block, if they are enabled in the theme.
- ...$args(mixed)
Some features (parameter $feature) can be additionally configured, and this parameter is needed for such configuration - it contains additional parameters for the function specified in the $feature parameter.
It only makes sense when a single feature (string, not an array) is specified in $feature.
Examples
#1 Examples of adding different features
add_post_type_support( 'my_post_type', 'comments' ); add_post_type_support( 'my_post_type', [ 'author', 'excerpt', ] ); add_post_type_support( 'my_post_type', 'my_feature', [ 'field' => 'value', ] );
#2 Support 'excerpt' for static pages
This example shows how to add the quote metabox to static pages (page post type). Such a box, for example, is present in posts but not in static pages. Add this code to functions.php:
add_action( 'init', 'my_custom_init' );
function my_custom_init(){
add_post_type_support( 'page', 'excerpt' );
}
#3 Adding additional meta-fields to an already registered post type
Suppose we have a post type book and we need to add the "custom fields" metabox to it, but only for the administrator. I.e. the authors should not see this metabox. It can be done by the following code:
add_action( 'init', 'wpkama_metaboxes_to_game' );
function wpkama_metaboxes_to_game(){
if( ! current_user_can( 'manage_options' ) ){
return;
}
add_post_type_support( 'book', ['custom-fields'] );
}
Notes
- Global. Array. $_wp_post_type_features
Changelog
| Since 3.0.0 | Introduced. |
| Since 5.3.0 | Formalized the existing and already documented ...$args parameter by adding it to the function signature. |
add_post_type_support() add post type support code WP 6.8.3
function add_post_type_support( $post_type, $feature, ...$args ) {
global $_wp_post_type_features;
$features = (array) $feature;
foreach ( $features as $feature ) {
if ( $args ) {
$_wp_post_type_features[ $post_type ][ $feature ] = $args;
} else {
$_wp_post_type_features[ $post_type ][ $feature ] = true;
}
}
}