$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Enable Authors in Multiple Custom Post Types|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Enable Authors in Multiple Custom Post Types

matteradmin11PV0评论

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far