add_post_type_support()WP 3.0.0

Register support of certain features for a post type.

All core features are directly associated with a functional area of the edit screen, such as the editor or a meta box. Features include: 'title', 'editor', 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', 'page-attributes', 'thumbnail', 'custom-fields', and 'post-formats'.

Additionally, the 'revisions' feature dictates whether the post type will store revisions, and the 'comments' feature dictates whether the comments count will show on the edit screen.

A third, optional parameter can also be passed along with a feature to provide additional information about supporting that feature.

Example usage:

add_post_type_support( 'my_post_type', 'comments' );
add_post_type_support( 'my_post_type', array(
	'author', 'excerpt',
) );
add_post_type_support( 'my_post_type', 'my_feature', array(
	'field' => 'value',
) );
1 time — 0.000021 sec (very fast) | 50000 times — 0.05 sec (speed of light)

No Hooks.

Return

null. Nothing.

Usage

add_post_type_support( $post_type, $feature, ...$args );
$post_type(string) (required)
The post type for which to add the feature.
$feature(string|array) (required)
The feature being added, accepts an array of feature strings or a single string.
...$args(mixed) (required)
Optional extra arguments to pass along with certain features.

Examples

0

#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',
] );
0

#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' );
}
0

#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.

Code of add_post_type_support() WP 6.0.1

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