Enable Authors in Multiple Custom Post Types

admin2025-06-03  8

I have found the below code to enable viewing the author of a custom post type and it works perfectly for one custom post type. But I need it to work for 4 custom post types called: detox, recipes, movements, lifestyle.

function add_author_support_to_posts() {
   add_post_type_support( 'your_custom_post_type', 'author' ); 
}
add_action( 'init', 'add_author_support_to_posts' );

What is the correct syntax to include all 4 custom post types?

I have found the below code to enable viewing the author of a custom post type and it works perfectly for one custom post type. But I need it to work for 4 custom post types called: detox, recipes, movements, lifestyle.

function add_author_support_to_posts() {
   add_post_type_support( 'your_custom_post_type', 'author' ); 
}
add_action( 'init', 'add_author_support_to_posts' );

What is the correct syntax to include all 4 custom post types?

Share Improve this question asked Feb 14, 2019 at 16:54 Shelley FinchShelley Finch 1 1
  • Could you not copy paste the line multiple times but put a different post type in each? You don't need to do it in the 1 function call – Tom J Nowell Commented Feb 14, 2019 at 17:40
Add a comment  | 

2 Answers 2

Reset to default 0

I would build a loop to pass all my cpt's into like so

function add_cpt_author_support( ) {

    $post_types = array('detox', 'recipes', 'movements', 'lifestyle');

    foreach ($post_types as $type) {
        add_post_type_support($type, 'author');
    }
}
add_action( 'init', 'add_cpt_author_support' );

One other thought though, why aren't you just declaring support for authors when you register your custom post types?

function add_author_support_to_posts() {
    $args = array(
       'public'   => true,
       '_builtin' => false
    );

    $output = 'names';

    $post_types = get_post_types( $args, $output ); 

    foreach ( $post_types  as $post_type ) {
        add_post_type_support( $post_type, 'author' );
    }

}
add_action( 'init', 'add_author_support_to_posts' );

Getting all custom post types as array with get_post_types function. Then running a foreach loop and adding the author support to custom post types

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748907123a314710.html

最新回复(0)