ページごとにnoindexとnofollowを設定する方法

WordPressサイトで特定のページや投稿を検索結果に出したくない場合や、リンク先に評価を渡したくない場合、noindex と nofollow をページ単位で設定する方法が便利です。
functions.php に簡単なカスタムコードを追加するだけで、管理画面から簡単に設定できます。

1 実装のポイント

  • 投稿・固定ページの編集画面で「Googleボット noindex」「Googleボット nofollow」をチェックするだけで設定可能
  • ページごとに noindex / nofollow を出し分け可能
  • 投稿一覧・固定ページ一覧にカスタム列を追加して、設定状況を確認できる
  • プラグイン不要で軽量

2 noindex と nofollow の違い

  • noindex: ページ自体を検索結果に表示させない
  • nofollow: ページ内のリンク先を検索エンジンがたどらないようにする
  • 両方を組み合わせて設定することも可能

3 functions.php にコードを追加


// ------------------------------
// 投稿・固定ページごとに Google ボットを noindex / nofollow するカスタムフィールド
// ------------------------------

// 投稿・固定ページ編集画面にチェックボックスを追加
add_action('add_meta_boxes', function() {
    add_meta_box(
        'robots_meta',
        'Googleボット制御',
        function($post){
            $noindex = get_post_meta($post->ID, '_noindex', true);
            $nofollow = get_post_meta($post->ID, '_nofollow', true);

            echo '<label>';
            echo '<input type="checkbox" name="noindex_checkbox" value="1" ' . checked(1, $noindex, false) . ' />';
            echo ' このページを検索結果に表示させない (noindex)';
            echo '</label><br>';

            echo '<label>';
            echo '<input type="checkbox" name="nofollow_checkbox" value="1" ' . checked(1, $nofollow, false) . ' />';
            echo ' このページのリンクをフォローさせない (nofollow)';
            echo '</label>';
        },
        ['post', 'page'],
        'side',
        'high'
    );
});

// 保存処理
add_action('save_post', function($post_id){
    if(isset($_POST['noindex_checkbox'])){
        update_post_meta($post_id, '_noindex', 1);
    } else {
        delete_post_meta($post_id, '_noindex');
    }

    if(isset($_POST['nofollow_checkbox'])){
        update_post_meta($post_id, '_nofollow', 1);
    } else {
        delete_post_meta($post_id, '_nofollow');
    }
});

// ヘッダー出力に noindex / nofollow を追加
add_action('wp_head', function(){
    if(is_singular()){
        global $post;
        $noindex = get_post_meta($post->ID, '_noindex', true);
        $nofollow = get_post_meta($post->ID, '_nofollow', true);

        $robots = [];
        if($noindex) $robots[] = 'noindex';
        if($nofollow) $robots[] = 'nofollow';

        if(!empty($robots)){
            echo '<meta name="robots" content="' . implode(',', $robots) . '">';
        }
    }
});

// 投稿一覧・固定ページ一覧にカスタム列を追加
add_filter('manage_post_posts_columns', function($columns){
    $columns['noindex'] = 'noindex';
    $columns['nofollow'] = 'nofollow';
    return $columns;
});
add_filter('manage_page_posts_columns', function($columns){
    $columns['noindex'] = 'noindex';
    $columns['nofollow'] = 'nofollow';
    return $columns;
});

// カスタム列にチェック状況を表示
add_action('manage_post_posts_custom_column', function($column_name, $post_id){
    if($column_name === 'noindex'){
        echo get_post_meta($post_id, '_noindex', true) ? '×' : ' ';
    }
    if($column_name === 'nofollow'){
        echo get_post_meta($post_id, '_nofollow', true) ? '×' : ' ';
    }
}, 10, 2);

add_action('manage_page_posts_custom_column', function($column_name, $post_id){
    if($column_name === 'noindex'){
        echo get_post_meta($post_id, '_noindex', true) ? '×' : ' ';
    }
    if($column_name === 'nofollow'){
        echo get_post_meta($post_id, '_nofollow', true) ? '×' : ' ';
    }
}, 10, 2);
    
補足:
- チェックを入れたページだけ noindex / nofollow が適用されます。
- 投稿一覧・固定ページ一覧に「×」で設定状況を確認可能です。
- トップページやカテゴリーページ、アーカイブページには適用されません。

4 ポイント

is_singular() は WordPress の 単一ページ判定関数 で、以下のときに「true」になります。

4.1 WordPress の is_singular() とは?

is_singular() は「単一ページかどうか」を判定する関数です。

4.2 is_singular() が true になるケース

  • 投稿(single post)
  • 固定ページ(page)
  • カスタム投稿タイプの個別ページ(custom post type single)

4.3 is_singular() が false になるケース

  • HOME(トップページ) → is_home()is_front_page() で判定
  • カテゴリーページ → is_category()
  • タグページ → is_tag()
  • 日付アーカイブ・投稿タイプアーカイブ
  • 検索結果ページ → is_search()

つまり is_singular() は「投稿・固定ページ・カスタム投稿の詳細ページかどうか」を判定する関数です。
トップページやアーカイブページには適用されないため、用途に応じて
is_front_page()is_category() などの条件分岐と組み合わせて使いましょう。

5 代用プラグイン

  • All in One SEO
  • Yoast SEO
  • Rank Math

6 まとめ

WordPressでページ単位・投稿単位で Google ボットの noindex / nofollow を設定するには、functions.php にカスタムフィールドを追加するだけで簡単に実現できます。
投稿一覧・固定ページ一覧にカスタム列を追加すれば、どのページに設定されているか一目で確認可能です。
SEOの調整や非公開ページの管理に非常に便利な手法です。