2026年5月20日

2026年5月20日

WordPressのサイトマップを設定する方法

はじめに

XMLサイトマップはGoogleなどの検索エンジンにサイト内のページ構造を伝えるファイルです。サイトマップを正しく設定・送信することで、新しい記事のインデックス登録が早まり、重要なページが見落とされにくくなります。

症状・原因

  • 新しい記事がいつまでもGoogleにインデックスされない
  • サイトマップのURLが見つからない
  • Google Search Consoleでサイトマップエラーが表示される
  • カスタム投稿タイプがサイトマップに含まれない

解決手順

ステップ1:WordPress標準サイトマップを確認する

WordPress 5.5以降、サイトマップ機能が標準搭載されています。

標準サイトマップのURL構成:
https://example.com/wp-sitemap.xml          ← インデックスサイトマップ
https://example.com/wp-sitemap-posts-post-1.xml     ← 投稿
https://example.com/wp-sitemap-posts-page-1.xml     ← 固定ページ
https://example.com/wp-sitemap-taxonomies-category-1.xml ← カテゴリ
https://example.com/wp-sitemap-taxonomies-post_tag-1.xml ← タグ
https://example.com/wp-sitemap-users-1.xml           ← 著者
// functions.php: 標準サイトマップの有効/無効
// 無効化する場合(Yoast SEO等を使う場合)
add_filter('wp_sitemaps_enabled', '__return_false');

ステップ2:サイトマップをカスタマイズする

// functions.php

// 著者サイトマップを除外(プライバシー保護)
add_filter('wp_sitemaps_add_provider', function ($provider, string $name): mixed {
    if ($name === 'users') {
        return false;
    }
    return $provider;
}, 10, 2);

// カスタム投稿タイプを追加
add_filter('wp_sitemaps_post_types', function (array $post_types): array {
    // 'product'(WooCommerce)をサイトマップに含める場合、
    // register_post_type で public:true にするだけで自動追加される

    // 特定の投稿タイプを除外する場合
    unset($post_types['attachment']); // メディアを除外
    return $post_types;
});

// タグアーカイブをサイトマップから除外
add_filter('wp_sitemaps_taxonomies', function (array $taxonomies): array {
    unset($taxonomies['post_tag']);
    return $taxonomies;
});

// サイトマップの最大エントリ数を変更(デフォルト2000)
add_filter('wp_sitemaps_max_urls', function (int $max_urls): int {
    return 1000;
});

// lastmod を追加する(デフォルトでは含まれない)
add_filter('wp_sitemaps_posts_entry', function (array $entry, WP_Post $post): array {
    $entry['lastmod'] = get_the_modified_date('c', $post);
    return $entry;
}, 10, 2);

add_filter('wp_sitemaps_taxonomies_entry', function (array $entry, WP_Term $term): array {
    // タームの最終更新日時は直接取得できないため、最新記事の更新日を使用
    $args  = [
        'post_type'      => 'post',
        'posts_per_page' => 1,
        'tax_query'      => [['taxonomy' => $term->taxonomy, 'field' => 'term_id', 'terms' => $term->term_id]],
        'orderby'        => 'modified',
    ];
    $posts = get_posts($args);
    if (!empty($posts)) {
        $entry['lastmod'] = get_the_modified_date('c', $posts[0]);
    }
    return $entry;
}, 10, 2);

ステップ3:noindexページをサイトマップから除外する

// functions.php

// noindexのページをサイトマップから除外
add_filter('wp_sitemaps_posts_query_args', function (array $args, string $post_type): array {
    // パスワード保護記事を除外
    $args['has_password'] = false;

    // カスタムフィールドでnoindexが設定されている記事を除外
    $args['meta_query'] = [
        'relation' => 'OR',
        ['key' => '_noindex', 'compare' => 'NOT EXISTS'],
        ['key' => '_noindex', 'value' => '1', 'compare' => '!='],
    ];

    return $args;
}, 10, 2);

ステップ4:Google Search Consoleに送信する

Google Search Console へのサイトマップ送信手順:

1. Search Console (https://search.google.com/search-console/) にアクセス
2. 左メニュー「インデックス作成」→「サイトマップ」を選択
3. サイトマップのURLを入力して「送信」
   - WordPress標準: https://example.com/wp-sitemap.xml
   - Yoast SEO:    https://example.com/sitemap_index.xml

4. ステータスが「成功」になることを確認
5. 「検出されたURL数」で重要ページが含まれているか確認

確認コマンド:
# サイトマップの内容を確認
curl -s https://example.com/wp-sitemap.xml | head -50

# Googleのクローラーでアクセス確認
curl -s -A "Googlebot/2.1" https://example.com/wp-sitemap.xml | grep '<loc>'

ステップ5:robots.txtにサイトマップを記載する

# .htaccess または WordPress管理画面「設定→表示設定」から編集
# robots.txt の内容:

User-agent: *
Disallow: /wp-admin/
Disallow: /wp-includes/
Allow: /wp-admin/admin-ajax.php

# サイトマップの場所を明示
Sitemap: https://example.com/wp-sitemap.xml
// functions.php: robots.txtにサイトマップを動的追加
add_filter('robots_txt', function (string $output): string {
    $output .= "\nSitemap: " . home_url('/wp-sitemap.xml') . "\n";
    return $output;
});

注意事項

  • noindex設定のページはサイトマップに含めないでください。Googleが混乱し、どちらの指示に従うべきか判断できなくなります
  • サイトマップのURLを変更した場合は、Google Search Consoleで古いサイトマップを削除して新しいURLを再送信してください
  • Search Consoleで「取得できませんでした」エラーが出る場合は、サーバーのアクセス制限(.htaccess、ファイアウォール)を確認してください

まとめ

WordPress 5.5以降は /wp-sitemap.xml が自動生成されます。wp_sitemaps_* フィルターでカスタム投稿タイプの追加・著者の除外・lastmodの付加などをカスタマイズし、Google Search Consoleに送信します。robots.txtにもSitemapディレクティブを記載してクローラーに場所を伝えます。

お気軽にご相談ください

お見積りへ お問い合わせへ