【WordPress】カスタムフィールドの値を投稿本文(post_content)にコピーする

WordPressでは、カスタムフィールドは通常検索対象になりません。
テンプレートなどを利用していて、検索機能が独自のカスタマイズされていると、カスタムフィールドの値を検索対象にする機能の追加は一苦労です。

カスタムフィールドの値を、投稿本文にコピーすると簡単に検索対象に出来ます。

CSVで一括投稿する場合や、ダッシュボードの投稿画面から情報を入力する場合に
カスタムフィールドの値を取得し、連結、投稿本文(post_content)にコピーするコードをfunctions.phpに追加します。

function update_post_content_with_custom_fields( $post_id ) {
    // 投稿の編集中でない場合や、カスタムフィールドの更新中である場合は処理しない
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
    if ( defined('DOING_AJAX') && DOING_AJAX ) return;
    if ( 'post' !== get_post_type( $post_id ) ) return;
    // 更新処理中フラグが設定されている場合は処理しない
    if ( get_post_meta( $post_id, '_updating_post_content', true ) ) return;
    // カスタムフィールドの値を取得します。
    $custom_fields = array(
        '項目1',
        '項目2',
        '項目3',
        '項目4',
        '項目5',
    );
    $post_content = '';
    // カスタムフィールドの値を連結します。
    foreach ( $custom_fields as $field_name ) {
        $field_value = get_post_meta( $post_id, $field_name, true );
        if ( ! empty( $field_value ) ) {
            $post_content .= "<strong>{$field_name}:</strong> {$field_value}<br>";
        }
    }
    // post_contentを更新します。
    if ( ! empty( $post_content ) ) {
        // 更新処理中フラグを設定
        update_post_meta( $post_id, '_updating_post_content', true );
        $post = array(
            'ID' => $post_id,
            'post_content' => $post_content,
        );
        // save_post アクションを一時的に無効化
        remove_action( 'save_post', 'update_post_content_with_custom_fields' );
        // 投稿を更新
        wp_update_post( $post );
        // save_post アクションを再度有効化
        add_action( 'save_post', 'update_post_content_with_custom_fields' );
        // 更新処理中フラグをクリア
        delete_post_meta( $post_id, '_updating_post_content' );
    }
}
// 投稿が保存される際に呼び出すアクションフックを追加します。
add_action( 'save_post', 'update_post_content_with_custom_fields' );

function copy_custom_fields_to_post_content_on_import( $post_id ) {
    // インポート中の投稿かどうかを確認
    if ( defined('WP_IMPORTING') && WP_IMPORTING ) {
        // カスタムフィールドの値を取得します。
        $custom_fields = array(
            '項目1',
            '項目2',
            '項目3',
            '項目4',
            '項目5',
        );
        $post_content = '';
        // カスタムフィールドの値を連結します。
        foreach ( $custom_fields as $field_name ) {
            $field_value = get_post_meta( $post_id, $field_name, true );
            if ( ! empty( $field_value ) ) {
                $post_content .= "<strong>{$field_name}:</strong> {$field_value}<br>";
            }
        }
        // post_contentを更新します。
        if ( ! empty( $post_content ) ) {
            $post = array(
                'ID' => $post_id,
                'post_content' => $post_content,
            );
            wp_update_post( $post );
        }
    }
}
// 投稿が保存される際に呼び出すアクションフックを追加します。
add_action( 'save_post', 'update_post_content_with_custom_fields' );
// インポート時にもカスタムフィールドの値を post_content にコピーするためのアクションフックを追加します。
add_action( 'import_post_created', 'copy_custom_fields_to_post_content_on_import' );

前半部分は投稿画面からの更新でのコピー。 後半部分はCSVインポート時のコピーです。

前半部分は単体で使えますが、後半部分は前半部分の機能を一部使っているのでそのままでは使えません。
CSVインポートの時だけ値をコピーする場合は少しカスタマイズが必要になります。

関連記事

コメントを残す

*