Set default title for Wordpress Custom Post Types?

2019-08-27 04:50发布

I have a Wordpress custom post type "Featured Book", which uses a subtitle to specify title of the book. Eg: "Featured Book: The Hunger Games"

Is it possible to automatically set the title of the post as "Featured Book" ?

I found this code through Googling but it does not work (Wordpress backend returns a blank page/white screen of death)

Is there some syntax error in the code, or is this just not possible?

function add_custom_title( $data, $postarr ) {
  if($data['post_type'] == 'featured-book') {
    if(empty($data['post_title'])) {
      $data['post_title'] = 'Featured Book';
    }
  }
  return $data;
}
add_filters('wp_insert_post_data', 'add_custom_title', 10, 2 );

标签: wordpress
1条回答
狗以群分
2楼-- · 2019-08-27 05:07

You've got a typo:

add_filter( 'wp_insert_post_data', 'add_custom_title', 10, 2 );

查看更多
登录 后发表回答