2026年10月7日

2026年10月7日

Advanced Adsのエラーを解決する方法

はじめに

Advanced Adsで広告を設定・公開したがサイトのどのページにも広告が表示されない・管理者ログイン中も広告が表示されてしまいクリックが計上されてしまう・特定カテゴリのページのみ広告を非表示にする設定が機能しない・GoogleアドセンスのAuto Adsと競合してレイアウトが崩れるといった問題は、表示条件・ユーザーロール設定・キャッシュが原因です。

症状・原因

  • Advanced Adsで広告を「公開」状態にしたが該当ページに広告のHTMLが出力されない
  • 管理者ユーザーでログインしているとAdSenseの広告が表示されクリックしてしまう
  • 特定カテゴリページでは広告を非表示に設定したが引き続き表示されている
  • A/Bテストを設定したが常に同じバリアントが表示される

解決手順

ステップ1:Advanced Adsの状態を確認する

# Advanced Adsの設定確認
wp eval "
if (defined('ADVADS_VERSION')) {
    echo 'Advanced Ads version: ' . ADVADS_VERSION . PHP_EOL;
}

// 広告一覧を確認
\$ads = get_posts([
    'post_type'      => 'advanced_ads',
    'post_status'    => ['publish', 'draft'],
    'posts_per_page' => 20,
]);

echo 'Total ads: ' . count(\$ads) . PHP_EOL;

foreach (\$ads as \$ad) {
    \$ad_obj = new Advanced_Ads_Ad(null, \$ad);
    printf('  #%d: %s | Type: %s | Status: %s' . PHP_EOL,
        \$ad->ID,
        \$ad->post_title,
        get_post_meta(\$ad->ID, 'advanced_ads_ad_type', true),
        \$ad->post_status
    );
}

// グローバル設定を確認
\$options = get_option('advanced-ads-settings', []);
echo 'Disable for logged in: '  . (!empty(\$options['disable-ads-for-users']) ? 'yes' : 'no') . PHP_EOL;
echo 'Tracking enabled: '       . (!empty(\$options['tracking']) ? 'yes' : 'no') . PHP_EOL;
echo 'Hide from admin: '        . (!empty(\$options['hide-for-admin']) ? 'yes' : 'no') . PHP_EOL;
"

ステップ2:広告の表示条件をカスタマイズする

// functions.php: Advanced Ads表示条件

// ① 広告の表示可否をプログラムで制御
add_filter('advanced-ads-can-display', function(bool $can_display, Advanced_Ads_Ad $ad): bool {
    // 管理者・編集者には広告を表示しない
    if (current_user_can('edit_posts')) {
        return false;
    }

    // ボットには広告を表示しない
    $user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
    $bot_patterns = ['Googlebot', 'bingbot', 'facebookexternalhit', 'Twitterbot'];
    foreach ($bot_patterns as $pattern) {
        if (str_contains($user_agent, $pattern)) {
            return false;
        }
    }

    return $can_display;
}, 10, 2);

// ② 特定の投稿タイプでは広告を非表示
add_filter('advanced-ads-can-display', function(bool $can_display, Advanced_Ads_Ad $ad): bool {
    global $post;
    if (!$post) return $can_display;

    // ランディングページには広告を表示しない
    $no_ads_pages = ['/landing/', '/thanks/', '/register/'];
    $current_path = parse_url(get_permalink($post->ID), PHP_URL_PATH);
    foreach ($no_ads_pages as $path) {
        if (str_contains($current_path, $path)) {
            return false;
        }
    }

    return $can_display;
}, 15, 2);

// ③ カスタム表示条件を登録
add_filter('advanced-ads-visitor-conditions', function(array $conditions): array {
    $conditions['is_premium_user'] = [
        'label'    => 'プレミアム会員',
        'callback' => function(): bool {
            if (!is_user_logged_in()) return false;
            return (bool) get_user_meta(get_current_user_id(), '_is_premium', true);
        },
        'negate'   => true,
    ];
    return $conditions;
});

ステップ3:広告グループとローテーションを設定する

// functions.php: 広告グループ管理

// ① A/Bテストの均等配分を確認
add_filter('advanced-ads-group-ad-weights', function(array $weights, int $group_id): array {
    // A/Bテストグループは等しい重みに
    $group = Advanced_Ads_Group::get_instance($group_id);
    if ($group && $group->type === 'default') {
        $count = count($weights);
        if ($count > 0) {
            $equal_weight = 100 / $count;
            return array_fill_keys(array_keys($weights), $equal_weight);
        }
    }
    return $weights;
}, 10, 2);

// ② 広告の表示回数を制限
add_filter('advanced-ads-ad-select-args', function(array $args): array {
    // 1ユーザーあたり1セッション3回まで表示
    if (!isset($args['output']['frequency'])) {
        $args['output']['frequency'] = [
            'count'  => 3,
            'period' => 'session',
        ];
    }
    return $args;
});

// ③ プレースメントの出力をカスタマイズ
add_filter('advanced-ads-placement-output', function(string $output, array $placement_data): string {
    // 広告ラッパーにカスタムクラスを追加
    $output = str_replace(
        'class="advanced-ads-ad-container"',
        'class="advanced-ads-ad-container my-custom-ad"',
        $output
    );
    return $output;
}, 10, 2);

ステップ4:インプレッション・クリック計測を修正する

# 広告統計の確認
wp eval "
// Advanced Ads統計データを確認
global \$wpdb;
\$stats = \$wpdb->get_results(
    \"SELECT ad_id, SUM(impressions) as total_impressions, SUM(clicks) as total_clicks
     FROM {\$wpdb->prefix}advanced_ads_stats
     GROUP BY ad_id
     ORDER BY total_impressions DESC LIMIT 10\"
);

echo 'Ad statistics:' . PHP_EOL;
foreach (\$stats as \$stat) {
    \$ad = get_post(\$stat->ad_id);
    printf('  Ad #%d (%s): %d impressions, %d clicks' . PHP_EOL,
        \$stat->ad_id,
        \$ad ? \$ad->post_title : 'deleted',
        \$stat->total_impressions,
        \$stat->total_clicks
    );
}

// キャッシュを確認・クリア
\$cache_key = 'advanced_ads_cache';
\$cached = get_transient(\$cache_key);
echo 'Cache exists: ' . (\$cached !== false ? 'yes' : 'no') . PHP_EOL;
delete_transient(\$cache_key);
echo 'Cache cleared' . PHP_EOL;
"

ステップ5:AdSenseとの競合を解消する

// functions.php: AdSense競合解消

// ① Auto Adsとの競合を防ぐためAdvanced Adsの出力タイミングを調整
add_filter('advanced-ads-output-inject-priority', function(int $priority): int {
    return 20; // デフォルト10から遅らせてAuto Adsより後に出力
});

// ② 特定広告IDに対してAdSenseの追加コードを出力
add_action('advanced-ads-ad-output-before', function(Advanced_Ads_Ad $ad): void {
    if (get_post_meta($ad->id, 'advanced_ads_ad_type', true) !== 'adsense') return;

    // AdSenseのpageビューを計測
    error_log(sprintf('[AdvAds] AdSense ad #%d displayed on %s', $ad->id, get_the_permalink()));
});

// ③ キャッシュプラグインとの連携設定
add_action('advanced-ads-pro-cache-busting', function(): void {
    // WP Rocketのキャッシュを広告表示後にクリア
    if (function_exists('rocket_clean_post')) {
        global $post;
        if ($post) rocket_clean_post($post->ID);
    }
});

// ④ ログインユーザーには広告の代わりにプロモーションを表示
add_filter('advanced-ads-output', function(string $output, Advanced_Ads_Ad $ad): string {
    if (!is_user_logged_in()) return $output;
    if (!current_user_can('edit_posts')) return $output;

    // 管理者・編集者向けにデバッグ情報を表示
    return sprintf('<div style="border:1px dashed #ccc;padding:5px;font-size:11px;color:#999">[Ad #%d: %s - hidden for editors]</div>',
        $ad->id, esc_html($ad->title));
}, 10, 2);

注意事項

  • Advanced Adsの広告が表示されない場合は「Advanced Ads → 設定 → 一般」で「管理者に広告を非表示」が有効になっているか確認してください。デフォルトでは有効になっていないため管理者でログインしながらテストすると広告が表示されます
  • キャッシュプラグイン(WP Rocket・W3 Total Cache等)を使用している場合、キャッシュされたページには広告のターゲティング条件(ログイン状態・閲覧履歴等)が適用されません。Advanced Ads ProのCache Bustingアドオンを使用するか、広告ブロックをキャッシュから除外してください
  • GoogleアドセンスのAuto Adsを有効にしている場合、Advanced Adsで設定した広告と重複して表示されることがあります。Auto Adsの自動配置エリアとAdvanced Adsの手動配置エリアが重ならないよう設計してください

まとめ

Advanced Ads修復は①advanced_ads投稿タイプでステータス・タイプ確認・advanced-ads-settingsオプションで管理者非表示・トラッキング設定を確認、②advanced-ads-can-displayフィルターで編集者・ボットを除外・ランディングページで非表示・advanced-ads-visitor-conditionsでカスタム表示条件追加、③advanced-ads-group-ad-weightsフィルターでA/Bテストを均等配分・表示回数制限・プレースメント出力のカスタマイズ、④advanced_ads_statsテーブルで計測データ確認・トランジェントキャッシュクリア、⑤出力タイミングをpriority=20に遅らせてAuto Adsとの競合解消・ログインユーザーにはデバッグ情報表示する手順で解決します。

お気軽にご相談ください

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