times / acf-to-wp-api

Puts all ACF fields from posts, pages, custom post types, attachments and taxonomy terms, into the WP-API output under the 'acf' key

Home Page:http://wordpress.org/plugins/acf-to-wp-api/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Filter by ACF fields value

pirony opened this issue · comments

Hi and thanks for this very helpfull plugin!!!
I’m facing an issue with the filtering of posts by custom fields value, using angular. Maybe you can help me?
I thought I could easily do it using:

../wp-json/posts/?type=some_custom_type&filter[X]=Y

But I cant get it to work. Do you know a way to do it?
Many thanks

Hey @pirony,

This is absolutely possible, however requires you to add the following to your functions.php file to allow filtering on the WP-API by meta_key.

add_filter( 'json_query_vars', 'filterJsonQueryVars' );

function filterJsonQueryVars( $vars ) {
    $vars[] = 'meta_key';
    return $vars;
}

Once you added that, you can filter using these arguments

.../wp-json/posts?filter[orderby]=meta_value_num&filter[meta_key]=order&filter[order]=ASC

Usage

  • filter[orderby]: Either meta_value or meta_value_num depending on whether you're filtering on an alphanumeric value or a numeric value
  • filter[meta_key]: The key you want to filter on, this is the name of the ACF field
  • filter[order]: The order to receive the data in, ASC or DESC

I may look at moving the above code into this plugin in the next version, so you don't have to add it to your functions.php file, but for now, that's what you'll have to do.

Hey @chrishutchinson,
thanks for your answer but I still can't get it to work...
I must be doin' it wrong. With my code, it might be easier to find the problem

Here is my search form directive:

app.directive('mySearchForm', function() {
    return {
        restrict: 'EA',
        templateUrl: myLocalized.partials + 'search-form.html',
        controller: ['$scope', '$http', function ( $scope, $http ) {
                       $scope.filter = {
                region: '',
                s:'',
                distance:''
            };
            $scope.search = function() {
                $http.get('wp-json/posts?type=events&filter[region]=' + $scope.filter.region + '&filter[s]=' + $scope.filter.s + '&filter[distance]=' + $scope.filter.distance).success(function(res){
                    $scope.posts = res;
                });
            };
        }]
    };
});

and here is my search-form template.

 <form role="search" ng-submit="search()">
        <label>
            Search for:
            <input type="search" name="s" ng-model="filter.s" />
        </label>
        <select class="browser-default" type="search" name="region" ng-model="filter.region">
            <option value="x">x</option>
            <option value="y">y</option>
            <option value="z">z</option>
        </select>
        <input type="text" name="distance" ng-model="filter.distance" />
        <input type="submit" value="Lancer la recherche" class="btn btn-large right" />
   </form>

thanks again for your time and help.

edit: the name of my ACF field is "distance".

Which fields are your custom fields setup in ACF, distance and region? Or just distance?

ahah too fast for me... i edited the previous post a bit late. Region is a custom taxonomy and works well.

 "meta": {
      "links": {
        ...
      }
    },
    "acf": {
      "distance": "4"
    },

An exemple of a post json datas, if it can help.

Ah, I think I was thinking about you ordering posts, rather than querying for a specific set.

Try this, which will query posts based on the meta key of 'distance' and the value of '4':

.../wp-json/posts?filter[meta_key]=distance&filter[meta_value]=4

I'm sorry, but unfortunately it still doesn't work...
The get request seems to be good in firebug.

.../wp-json/posts?type=events&filter[meta_key]=distance&filter[meta_value]=4

EDIT:
I finally got it to work:

I changed

add_filter( 'json_query_vars', 'filterJsonQueryVars' );

function filterJsonQueryVars( $vars ) {
    $vars[] = 'meta_key';
    return $vars;
}

to

add_filter( 'json_query_vars', 'filterJsonQueryVars' );

function filterJsonQueryVars( $vars ) {
    $vars[] = 'meta_value';
    return $vars;
}

And now it works like a charm.
Thanks a lot .Keep up the good work!!!

Yep - have just been testing and spotted that, I think it's valuable to add both meta_key and meta_value, but otherwise you should be good to go! Enjoy the plugin!

commented

Is it possible to filter by a nested meta key and value? How would i do this?

For example i have 3 scenarios:

"acf": {
    "key_1": "value",
    "key_2": {
        "sub_key_1": "subvalue",
        "sub_key_2": "subvalue2"
    },
    "key_3":[
        {
            "sub_object_id": "1",
            "sub_key": "yes"
        },
        {
            "sub_object_id": "2",
            "sub_key": "no"
        }
    ]
}

Filtering key_1 works fine:
/wp-json/posts?type=customtype&filter[meta_key]=key_1&filter[meta_value]=value

But i can't find a working result for key_2 and i'm not sure how to access key_3 sub objects. I've tried:
filter[meta_key]=key_2.sub_key
filter[meta_key]=key_2_sub_key
filter[meta_key]=key_2[sub_key]

Is there anything i'm missing?

Thanks

@yougotashovel did you figure this out at all? I can't find any way to do it easily.

This is still linked from the FAQ at https://wordpress.org/plugins/acf-to-wp-api/faq/ but doesn't seem to work in recent versions of Wordpress. I found this thread in the plugin's support forum and updated it with my working solution: https://wordpress.org/support/topic/filter-not-filtering

@yougotashovel Did you find out how to do this? I'm running into the same problem right now.

commented

@dubstrike don't think i got it to work this way, had to restructure and simplify requests

i am in a similar situation. this solution doesnt work on wordpress 4.9.2 and the one linked by @mattboutet didnt work for me either

never mind. i ended up doing a custom api endpoint for this. thanks.