0

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:

  1. I try to get support with the ACF devs, no help is provided about it;
  2. 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;
  3. 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.

1

Advanced Custom Fields stores values for multiple select fields as a serialised Array. For a select field with options 'one' and 'three' selected, the value stored on the database would look like this:

'a:2:{i:0;s:3:"one";i:1;s:5:"three";}'

It is simply not possible with WP_Query (which is where your meta query ends up) or SQL to filter results based on selected values stored this way. MySQL sees this value as a generic string, and cannot understand that there are distinct values contained in it ('one' or 'three', let alone query based on them. This is because serialisation occurs entirely in PHP and MySQL doesn't know about it. It's the same as if you stored JSON in a MySQL column: MySQL wouldn't know anything about the data structure. It's just text.

You either need to replace ACF with a solution that lets you store data the way you want (a custom taxonomy would be the simplest option), or use ACF's APIs & filters to change the way it stores data. You will need to refer to ACF resources and documentation to figure out how to do this, as it would be off-topic here (this question, as asked, is off-topic too, but I'm treating it as a question about meta_query and serialised data).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.