I'm suffering for weeks trying make a GET
call in the WP REST API
where I have a field in the ACF
plug-in with multiple select, and the stored value is an array()
.
Example of the response after the call in /wp-json/wp/v2/posts
{
...
},
...
"acf": {
...
"ddd": [
"11",
"12",
"13",
"14"
],
...
...
}
And the response when I call in /wp-json/acf/v3/posts
[
{
...
"acf": {
...
"ddd": [
"11",
"12",
"13",
"14"
],
...
}
}
]
So, the problem is when I use this function in functions.php
of my theme:
add_filter( 'rest_post_query', function( $args ) {
$args['meta_query'] = array(
array(
'key' => 'ddd',
'value' => esc_sql( $_GET['ddd'] ),
)
);
return $args;
} );
I'm able to filter like this: /wp-json/acf/v3/posts?fieldname=value
and I perfect give the posts filtered by the terms in the URL, but this works for simple fields. In my specific need the field ddd
is an array, because in the ACF I'm working with a multiple selection field. So when I try to filter using the function above I just get a empty response: []
.
I've tried too to use the Tags
field from WordPress Core
, what is a array
value in REST
response too, but I never managed to make it work, even using the deprecated function filter[key]=&filter[value]=
from WordPress
.
Useful informations:
- I try to get support with the
ACF
devs, no help is provided about it;- I use the
ACF to REST API
plug-in to expose the ACF fields to the REST API and the dev is trying to help my with this case;- My WordPress is 4.9.2 and PHP are 7.1.12 version.
I don't necessarily need to just go the way of filtering the call using the URL, it might be a different logic to do this in PHP with some pre-loop to be able to filter only posts with some field values like conditions, eg age, region, sex ... because they will be multiple filters at the same time.