0

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?

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
    Feb 14 '19 at 17:40
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?

1
  • Perfect! Thanks very much. Not sure what you mean by "declaring support for authors". My theme offers custom post types so I have just set them up there. Feb 17 '19 at 21:32
0
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.