実現したいこと
ワードプレスのカスタム投稿で登録した製品情報を、APIを使用して他のサイトにも同じように表示させたい
発生している問題・分からないこと
カタログサイトのようになっていて、既存サイトをもとに新しいサイトを構築している。
レイアウトなどもそのままでいいので、新しいサイトでも全く同じ情報が同じように表示させたい。
一覧画面と詳細画面の表示まではできたが、製品情報をACFとカスタムタクソノミーで入力しており、カスタムタクソノミーの情報だけ表示されないエラーが出ている。
また、API関連のコードを追加してから、元サイトの製品登録画面で、タクソノミーで登録したものがすべて表示されなくなった。
エラーメッセージ
error
1エラーのメッセージが出ているというよりは、表示されないというエラー
該当のソースコード
PHP
1add_action('rest_api_init', function () { 2 register_rest_route('catalog-api/v1', '/products', [ 3 'methods' => 'GET', 4 'callback' => 'catalog_api_get_products', 5 ]); 6}); 7 8function catalog_api_get_products(WP_REST_Request $request) 9{ 10 $manufacturer = $request->get_param('manufacturer'); 11 12 $args = [ 13 'post_type' => 'product', 14 'posts_per_page' => -1, 15 'meta_query' => [], 16 ]; 17 18 // 🔥 manufacturer(ACF)で絞り込み 19 if (!empty($manufacturer)) { 20 $args['meta_query'][] = [ 21 'key' => 'manufacturer', 22 'value' => $manufacturer, 23 'compare' => 'LIKE', // ← ここが超重要 24 ]; 25 } 26 27 $query = new WP_Query($args); 28 $results = []; 29 30 while ($query->have_posts()) { 31 $query->the_post(); 32 $id = get_the_ID(); 33 34 $results[] = [ 35 'id' => $id, 36 'title' => get_the_title(), 37 'slug' => get_post_field('post_name', $id), 38 'thumbnail' => get_the_post_thumbnail_url($id, 'large'), 39 'acf' => get_fields($id), 40 ]; 41 } 42 43 wp_reset_postdata(); 44 45 return $results; 46} 47 48 49add_filter('post_type_link', function ($link, $post) { 50 if ($post->post_type === 'product') { 51 return home_url('/product/' . $post->ID . '/'); 52 } 53 return $link; 54}, 10, 2); 55 56/** 57 * 製品 単品取得API 58 * /wp-json/catalog-api/v1/products/{id} 59 */ 60add_action('rest_api_init', function () { 61 register_rest_route('catalog-api/v1', '/products/(?P<id>\d+)', [ 62 'methods' => 'GET', 63 'callback' => 'catalog_get_single_product', 64 'permission_callback' => '__return_true', 65 ]); 66}); 67 68function catalog_get_single_product($request) { 69 $product_id = (int) $request['id']; 70 71 if (!$product_id) { 72 return new WP_Error('invalid_id', 'Invalid product ID', ['status' => 400]); 73 } 74 75 $post = get_post($product_id); 76 77 if (!$post || $post->post_type !== 'product') { 78 return new WP_Error('not_found', 'Product not found', ['status' => 404]); 79 } 80 81 // タクソノミー取得 82 $industry_terms = get_the_terms($post->ID, 'industry'); 83 $purpose_terms = get_the_terms($post->ID, 'purpose'); 84 85 $industry = []; 86 $purpose = []; 87 88 if (!is_wp_error($industry_terms) && !empty($industry_terms)) { 89 foreach ($industry_terms as $term) { 90 $industry[] = $term->name; 91 } 92} 93 94if (!is_wp_error($purpose_terms) && !empty($purpose_terms)) { 95 foreach ($purpose_terms as $term) { 96 $purpose[] = $term->name; 97 } 98} 99 100 return [ 101 'id' => $post->ID, 102 'title' => get_the_title($post), 103 'slug' => $post->post_name, 104 'permalink' => get_permalink($post), 105 'thumbnail' => get_the_post_thumbnail_url($post, 'thumbnail'), 106 'acf' => get_fields($product_id) ?: [], 107 'industry' => $industry, 108 'purpose' => $purpose, 109 110 ]; 111} 112
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
AIを活用して何度も修正してみたが改善されなかった。
補足
特になし
あなたの回答
tips
プレビュー