WordPressのカスタム投稿タイプ「news」でタイトルをカスタムフィールドの値に置き換える方法

WordPress
WordPress
B!

はじめに

WordPressのカスタム投稿タイプ news において、標準のタイトルではなく、カスタムフィールドで設定したタイトルをすべての箇所(一覧・個別記事・ウィジェット・SEOタイトルなど)で適用 したい場合があります。

本記事では、その方法を解説します。


実装方法

1. the_title フィルターを使い、テンプレートのタイトルを変更する

まず、the_title フィルターを利用し、テーマテンプレート内で the_title() を使用している箇所のタイトルをカスタムフィールドの値に置き換えます。

functions.php に追加

function custom_news_title_filter($title, $post_id) {
    // 現在の投稿情報を取得
    $post = get_post($post_id);
    
    // 投稿タイプが 'news' の場合のみ適用
    if ($post && $post->post_type === 'news') {
        // カスタムフィールドの値を取得(カスタムフィールド名が 'custom_news_title' の場合)
        $custom_title = get_post_meta($post_id, 'custom_news_title', true);

        // カスタムタイトルが設定されていれば、デフォルトタイトルを置き換え
        if (!empty($custom_title)) {
            return $custom_title;
        }
    }

    return $title; // それ以外は元のタイトルを返す
}

// the_title フィルターを適用
add_filter('the_title', 'custom_news_title_filter', 10, 2);

解説

  • the_title フィルターを使用し、WordPressの すべての the_title() 出力箇所 に影響を与える。
  • get_post_meta($post_id, 'custom_news_title', true) でカスタムフィールドからタイトルを取得。
  • カスタムタイトルが設定されている場合のみ置き換える
  • post_type === 'news' の場合のみ適用することで、他の投稿タイプには影響を与えない。

2. SEO用の <title> タグを変更する

the_title のフィルターだけでは、ブラウザのタブに表示される <title> タグの内容は置き換わりません。document_title_parts フィルターを使って、SEOタイトルも変更しましょう。

functions.php に追加

function custom_news_meta_title($title) {
    if (is_singular('news')) { // カスタム投稿タイプ 'news' の個別ページの場合
        global $post;
        $custom_title = get_post_meta($post->ID, 'custom_news_title', true);

        // カスタムタイトルがある場合、ブラウザのタイトルタグを置き換え
        if (!empty($custom_title)) {
            $title['title'] = $custom_title;
        }
    }
    return $title;
}
add_filter('document_title_parts', 'custom_news_meta_title');

解説

  • document_title_parts フィルターを使い、news の個別投稿ページの <title> タグを変更。
  • カスタムタイトルが設定されている場合のみ置き換える

3. Yoast SEO / All in One SEO 用のカスタムタイトル設定

Yoast SEO や All in One SEO を使っている場合は、専用のフィルターを利用してSEOタイトルを変更できます。

Yoast SEO の場合
function custom_news_yoast_seo_title($title) {
    if (is_singular('news')) {
        global $post;
        $custom_title = get_post_meta($post->ID, 'custom_news_title', true);
        
        if (!empty($custom_title)) {
            return $custom_title;
        }
    }
    return $title;
}
add_filter('wpseo_title', 'custom_news_yoast_seo_title');
All in One SEO の場合
function custom_news_aioseo_title($title) {
    if (is_singular('news')) {
        global $post;
        $custom_title = get_post_meta($post->ID, 'custom_news_title', true);
        
        if (!empty($custom_title)) {
            return $custom_title;
        }
    }
    return $title;
}
add_filter('aioseop_title', 'custom_news_aioseo_title');

解説

  • wpseo_title(Yoast SEO)や aioseop_title(All in One SEO)のフィルターを使い、SEOプラグインが出力するタイトルタグをカスタムフィールドの値に置き換える

4. 管理画面の投稿一覧のタイトルも変更する(オプション)

もし 管理画面の「投稿一覧」でもカスタムタイトルを表示したい 場合、以下のコードを追加します。

function custom_admin_news_title($column, $post_id) {
    if ($column === 'title') {
        $custom_title = get_post_meta($post_id, 'custom_news_title', true);
        if (!empty($custom_title)) {
            echo esc_html($custom_title);
        }
    }
}
add_action('manage_news_posts_custom_column', 'custom_admin_news_title', 10, 2);

解説

  • manage_news_posts_custom_column フィルターを利用して、管理画面の投稿一覧でもカスタムタイトルを表示

まとめ

the_title フィルターで、テーマテンプレート内のタイトルをカスタムフィールドに置き換え
document_title_parts で、ブラウザの <title> タグを変更
Yoast SEO / All in One SEO に対応し、SEOタイトルもカスタマイズ
管理画面の投稿一覧でもカスタムタイトルを表示可能

この方法を適用すれば、news のカスタム投稿タイプのタイトルを一括でカスタマイズし、一貫した表示が可能になります

B!
← 一覧へ戻る