サポート » テーマ » カスタムフィールドの値を記事のタイトルへ反映させたい

  • (前提)
    ■Advanced Custom Fieldsで作成したカスタムフィールドで、フィールド名が「question」
    ■カスタム投稿ではなく通常の投稿
    ■記事のタイトルは不要のため、以下の記述をfunctionへ記入済み

    add_action( 'init' , 'my_remove_post_editor_support' );
    function my_remove_post_editor_support() {
    remove_post_type_support( 'post', 'title' );//タイトル
    }

    上記の条件で、カスタムフィールドの値を記事のタイトルへ反映させたいですのですが上手くいかず困っております。
    記述した内容は以下の通りです。

    function replace_post_title($title) {
            global $post;
            //post_typeを判定(post, page, カスタム投稿)
            if( $post->post_type == 'post' ){
                if(get_field('question',$post->ID)){
                    $title = get_field('question',$post->ID);
                }
            }
            return $title;
        }
        add_filter('title_save_pre', 'replace_post_title');

    投稿の画面にも
    Warning: Attempt to read property “post_type” on null in・・・
    Warning: Cannot modify header information – headers already sent by・・・
    のエラーが出てしまいます。

    どなたかご教授をお願いいたしますm(__)m

  • トピック「カスタムフィールドの値を記事のタイトルへ反映させたい」には新たに返信することはできません。