wp_robots_noindex_search()


WordPressのwp_robots_noindex_search()関数は、検索結果ページにnoindexタグを追加するために使用されます。これにより、検索エンジンが検索結果ページをインデックスしないように指示します。

構文

wp_robots_noindex_search( bool $noindex );
  • $noindex(bool) — noindexタグを追加するかどうかを指定します。デフォルトはtrueです。

例1: 検索結果ページにnoindexタグを追加

検索結果ページにnoindexタグを追加する基本的な例です。

add_filter( 'wp_robots', 'wp_robots_noindex_search' );

例2: 検索結果ページのnoindexタグを無効化

検索結果ページにnoindexタグを追加しないようにする例です。

add_filter( 'wp_robots', function( $robots ) { return wp_robots_noindex_search( false ); } );

例3: 特定の条件下でnoindexタグを追加

特定の条件下でのみ検索結果ページにnoindexタグを追加する例です。

add_filter( 'wp_robots', function( $robots ) { if ( is_search() && ! is_user_logged_in() ) { return wp_robots_noindex_search(); } return $robots; } );

例4: noindexタグをカスタマイズ

noindexタグをカスタマイズして他のロボットメタタグと一緒に追加する例です。

add_filter( 'wp_robots', function( $robots ) { $robots['noindex'] = true; $robots['nofollow'] = true; return $robots; } );

例5: 検索結果ページ以外にnoindexタグを追加

検索結果ページ以外の特定のページにnoindexタグを追加する例です。

add_filter( 'wp_robots', function( $robots ) { if ( is_page( '特定のページ' ) ) { return wp_robots_noindex_search(); } return $robots; } );

例6: 複数の条件でnoindexタグを追加

複数の条件を組み合わせてnoindexタグを追加する例です。

add_filter( 'wp_robots', function( $robots ) { if ( is_search() || is_archive() ) { return wp_robots_noindex_search(); } return $robots; } );

例7: noindexタグを追加する前に他のフィルターを適用

noindexタグを追加する前に他のフィルターを適用する例です。

add_filter( 'wp_robots', function( $robots ) { $robots = apply_filters( 'custom_robots_filter', $robots ); return wp_robots_noindex_search( true ); } );

注意点

  • noindexタグを追加すると、検索エンジンがそのページをインデックスしなくなるため、検索結果に表示されなくなります。
  • 検索結果ページにnoindexタグを追加する場合は、SEOに影響を与える可能性があるため、慎重に使用してください。

関連機能: