HOME > WordPress> function.php

自動でタイトルを変更

この方法では、初回はタイトルを保存できない。
別の方法を探したいがなかなかうまくいかない。。^_^;

function replace_post_data($data, $postarr){
  if ( $data[‘post_type’] == ‘nippo’) {
        $author = $data[‘post_author’];
        $author = get_userdata($author);
        $name = $author->display_name;
        $data[‘post_title’] = $name.’の日報’;
    }
    //名刺申請の社員名をtitleに入れたかったが、wordpress4.4よりcreating default object from empty valueエラーが出て、正常に利用出来なくなったため削除
/*if( $data[‘post_type’]  == ‘pmeishi’ ) {
    global $post;
    $new_post = get_post( $post_id );
    $content = $new_post->post_content;
  $custom_fields = get_post_custom($post_id);
    $title = $custom_fields[‘社員名’][0];
    $data[‘post_title’] = $title;
    }*/
    return $data;
}
add_filter(‘wp_insert_post_data’, ‘replace_post_data’, ’99’, 2);