如何通过ACF属性查询Wordpress API?

时间:2018-08-05 14:29:34

标签: wordpress advanced-custom-fields wordpress-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"}

这些查询都不起作用:

/wp-json/wp/v2/hotels?acf={stad:{value:%27barcelona%27}}

/wp-json/wp/v2/hotels?acf.stad.value=barcelona

如何构建正确的URL查询?

如果这不可能,那将是替代解决方案?

2 个答案:

答案 0 :(得分:1)

我认为无法直接通过发布元过滤API请求。因为最后,“按ACF值过滤”(至少在帖子上)等于“按帖子元过滤”。

为此,您需要为自己的查询创建自己的REST处理程序。 Google进行的有关“向Wordpress API添加新路由”的快速搜索将提供很多示例,例如this one

答案 1 :(得分:0)

看看ACF(高级自定义字段)插件+ ACF-TO-REST-API插件:

https://wordpress.org/plugins/acf-to-rest-api/

https://github.com/airesvsg/acf-to-rest-api

相关问题