質問するログイン新規登録

Q&A

0回答

100閲覧

WEBサイトのAPI連携について

matsu766

総合スコア1

WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

0グッド

0クリップ

投稿2025/12/23 00:59

0

0

実現したいこと

ワードプレスのカスタム投稿で登録した製品情報を、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を活用して何度も修正してみたが改善されなかった。

補足

特になし

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.29%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問