1

I'm using the JSON Rest API plugin (https://wordpress.org/plugins/json-rest-api/). I have two custom taxonomies with one term each:

  1. Inner Part Categories
    • Eye
  2. Symptoms Categories (Man)
    • Broken Bone Single Fracture

I need to show posts that match both of these categories and terms. I've tried the following:

http://mcsc.hawkhorne.com/wp-json/posts?type=male-conditions&filter[taxonomy]=inner-part-categories&filter[term]=Eye&filter[taxonomy]=symptoms-categories-man&filter[term]=Broken%20Bone%20Single%20Fracture

But this method only returns the posts under the last declared taxonomy and term. For example, if I switch the tax and term around as such:

http://mcsc.hawkhorne.com/wp-json/posts?type=male-conditions&filter[taxonomy]=symptoms-categories-man&filter[term]=Broken%20Bone%20Single%20Fracture&filter[taxonomy]=inner-part-categories&filter[term]=Eye

It will then only show posts under the tax inner-part-categories and term Eye and ignore the first tax and term query.

I've been searching for hours and having found a solution.

I was also thinking maybe an array of sorts could be passed in the ajax data request, sort of how it's done in WP Query. I have the following code:

data: { filter: { 'posts_per_page': -1, 'post_status': 'publish', 'orderby': 'title', 'order': 'ASC' } },

Thanks for your help.

1

Firstly, your problem with the last post type and/or term overwriting all previous is because 'filter' is a 2-dimensional array. Therefore 'taxonomy' (the key in this case) can only have one value. If that seems odd, read this super helpful page on the plugin's github that helped me a lot (read this)

tldr; term lists and multi-dimensional arrays are possible with WP JSON API! you would just do type[]=post_type1&type[]=post_type2 for custom post types! Try it.

So, with that in mind... you still can't do a multiple custom taxonomy query currently (as far as I can tell). Bummer, I know. My solution? Nest your second query within the success callback of the first, then concatenate the arrays and sort the final array.

$http.get(
  $scope.api + '/posts?type=eats&filter[taxonomy]=eats-categories&filter[term]=eats-video'
).
success(function(data, status, headers, config){
  $scope.posts = data;
    $http.get(
      $scope.api + '/posts?type=shreds&filter[taxonomy]=shreds-categories&filter[term]=shreds-video'
    ).
    success(function(data_2, status, headers, config){
      $scope.posts = $scope.posts.concat( data_2 );
    }).
    error(function(data_2, status, headers, config){
        alert( 'video 2 widget query error' );
    });     
}).
error(function(data, status, headers, config){
    alert( 'video widget query error' );
});  

Note, this code is specific to my queries, but I tested it and it worked. I haven't written the sort function yet, but I'll either be sorting by the WordPress date field of the array values or using an angular filter to do something fun.

Edit: made a couple of changes for clarity.

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.