如何通过ACF属性查询Wordpress API?
How to query the Wordpress API by ACF properties?
我正在尝试根据 ACF 属性查询 Worpress API。
如果我不包含查询,我会得到以下输出:
[{"id":215,"date":"2018-08-05T09:21:37","date_gmt":"2018-08-5T08:21:37","guid":{"rendered":"x"},"modified":"2018-08-05T09:21:37",
"modified_gmt":"2018-08-05T08:21:37","slug":"sasradisson","status":"publish","type":"hotel","link":"https:x",
"title":{"rendered":"SAS Radisson"},"template":"","acf":{"stad":{"value":"barcelona","label":"barcelona"},
"description":"","images":false},"_links":{"self":[{"href":"x"}],"collection":[{"href":"x"}],
"about":[{"href":"x"}],"wp:attachment":[{"href":"x"}],"curies":[{"name":"wp","href":"x","templated":true}]}},
{"id":205,"date":"2018-08-04T11:25:35","date_gmt":"2018-08-04T10:25:35","guid":{"rendered":"x"},
"modified":"2018-08-04T14:22:12","modified_gmt":"2018-08-04T13:22:12","slug":"citybox-oslo","status":"publish",
"type":"hotel","link":"x","title":{"rendered":"Citybox Oslo"},"template":"",
"acf":{"stad":{"value":"oslo","label":"oslo"},"description":"","images":false},
"_links":{"self":[{"href":"x"}],"collection":[{"x"}],"about":[{"href":"x"}],"wp:attachment":[{"href":"x"}],
"curies":[{"name":"wp","href":"x","templated":true}]}}]
(为简洁起见,我将所有网址替换为 "x")
在所有其他属性中,有 ACF 属性,如下所示:
"acf":{"stad":{"value":"barcelona","label":"barcelona"}
None 这些查询有效:
/wp-json/wp/v2/hotels?acf={stad:{value:%27barcelona%27}}
/wp-json/wp/v2/hotels?acf.stad.value=barcelona
如何构建正确的 URL 查询?
如果这不可行,替代解决方案是什么?
我认为不可能直接使 API 请求被 post 元过滤。因为最终 "filtering by ACF values"(至少在 post 上)等于 "filtering by post meta"。
为此,您需要为自己的查询创建自己的 REST 处理程序。快速 Google 搜索 "adding new routes to Wordpress API" 将提供很多示例,如 this one.
查看 ACF(高级自定义字段)插件 + ACF-TO-REST-API 插件:
我正在尝试根据 ACF 属性查询 Worpress API。
如果我不包含查询,我会得到以下输出:
[{"id":215,"date":"2018-08-05T09:21:37","date_gmt":"2018-08-5T08:21:37","guid":{"rendered":"x"},"modified":"2018-08-05T09:21:37",
"modified_gmt":"2018-08-05T08:21:37","slug":"sasradisson","status":"publish","type":"hotel","link":"https:x",
"title":{"rendered":"SAS Radisson"},"template":"","acf":{"stad":{"value":"barcelona","label":"barcelona"},
"description":"","images":false},"_links":{"self":[{"href":"x"}],"collection":[{"href":"x"}],
"about":[{"href":"x"}],"wp:attachment":[{"href":"x"}],"curies":[{"name":"wp","href":"x","templated":true}]}},
{"id":205,"date":"2018-08-04T11:25:35","date_gmt":"2018-08-04T10:25:35","guid":{"rendered":"x"},
"modified":"2018-08-04T14:22:12","modified_gmt":"2018-08-04T13:22:12","slug":"citybox-oslo","status":"publish",
"type":"hotel","link":"x","title":{"rendered":"Citybox Oslo"},"template":"",
"acf":{"stad":{"value":"oslo","label":"oslo"},"description":"","images":false},
"_links":{"self":[{"href":"x"}],"collection":[{"x"}],"about":[{"href":"x"}],"wp:attachment":[{"href":"x"}],
"curies":[{"name":"wp","href":"x","templated":true}]}}]
(为简洁起见,我将所有网址替换为 "x")
在所有其他属性中,有 ACF 属性,如下所示:
"acf":{"stad":{"value":"barcelona","label":"barcelona"}
None 这些查询有效:
/wp-json/wp/v2/hotels?acf={stad:{value:%27barcelona%27}}
/wp-json/wp/v2/hotels?acf.stad.value=barcelona
如何构建正确的 URL 查询?
如果这不可行,替代解决方案是什么?
我认为不可能直接使 API 请求被 post 元过滤。因为最终 "filtering by ACF values"(至少在 post 上)等于 "filtering by post meta"。
为此,您需要为自己的查询创建自己的 REST 处理程序。快速 Google 搜索 "adding new routes to Wordpress API" 将提供很多示例,如 this one.
查看 ACF(高级自定义字段)插件 + ACF-TO-REST-API 插件: