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

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

実装のポイント

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

noindex と nofollow の違い

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

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 が適用されます。
- 投稿一覧・固定ページ一覧に「×」で設定状況を確認可能です。
- 全ページに適用したい場合はプラグインで設定する方が簡単です。

代用プラグイン

  • All in One SEO
  • Yoast SEO
  • Rank Math

まとめ

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