Search code examples
wordpressadvanced-custom-fieldswordpress-rest-api

How to query the Wordpress API by ACF properties?


I'm trying to query the Worpress API based on ACF properties.

If I don't include a query, I get this output:

[{"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}]}}]

(I replaced all urls with "x" for brevity)

Among all other properties, there are ACF properties, like this:

"acf":{"stad":{"value":"barcelona","label":"barcelona"}

None of these queries work:

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

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

How do I build a correct URL query?

If that isn't possible, what would be an alternate solution?


Solution

  • I believe it is not directly possible to make API request filtered by post meta. Because in the end, "filtering by ACF values" (on a post at least), equals "filtering by post meta".

    To do this, you'll need to create your own REST handler for your own queries. A quick Google search about "adding new routes to Wordpress API" will provide a lot of examples, as this one.