/** * company_detail URLでカテゴリ34(印刷会社詳細)以外の投稿へのアクセスを404にする * 画像やリビジョンなど、誤ったURLがインデックスされる問題を防ぐ * Added: 2026-01-22 */ function block_invalid_company_detail_urls() { if (is_admin()) return; $request_uri = $_SERVER['REQUEST_URI']; // /company_detail/ で始まるURLの場合のみ処理 if (strpos($request_uri, '/company_detail/') !== false) { // 数字のIDを抽出 if (preg_match('/\/company_detail\/(\d+)\/?/', $request_uri, $matches)) { $post_id = intval($matches[1]); // そのIDが「印刷会社詳細」カテゴリ(ID:34)に属しているか確認 if (!has_category(34, $post_id)) { // 404を返す global $wp_query; $wp_query->set_404(); status_header(404); nocache_headers(); include(get_404_template()); exit; } } } } add_action('template_redirect', 'block_invalid_company_detail_urls', 1); /** * より早い段階でcompany_detail URLの不正アクセスをブロック * parse_requestはtemplate_redirectより前に実行される * Added: 2026-01-22 */ function block_invalid_company_detail_early($wp) { if (is_admin()) return; $request_uri = $_SERVER['REQUEST_URI']; // /company_detail/数字/ のパターンをチェック if (preg_match('/\/company_detail\/(\d+)\/?/', $request_uri, $matches)) { $post_id = intval($matches[1]); // 投稿が存在し、カテゴリ34に属しているか確認 $post = get_post($post_id); if (!$post || $post->post_type !== 'post' || !has_category(34, $post_id)) { status_header(404); nocache_headers(); include(get_404_template()); exit; } } } add_action('parse_request', 'block_invalid_company_detail_early', 1); /** * 最も早い段階でcompany_detail URLの不正アクセスをブロック * initはWordPressの初期化時に実行される * Added: 2026-01-22 */ function block_invalid_company_detail_init() { if (is_admin()) return; $request_uri = $_SERVER['REQUEST_URI']; // /company_detail/数字/ のパターンをチェック if (preg_match('/\/company_detail\/(\d+)\/?/', $request_uri, $matches)) { $post_id = intval($matches[1]); // 投稿が存在し、post_typeがpostで、カテゴリ34に属しているか確認 global $wpdb; // post_typeを確認 $post_type = $wpdb->get_var($wpdb->prepare( "SELECT post_type FROM {$wpdb->posts} WHERE ID = %d", $post_id )); if ($post_type !== 'post') { status_header(404); nocache_headers(); echo '