5

I'm having some difficulty understanding syntax to make a call from my application to the wordpress REST API when it it comes to getting pages that contain multiple tags.

I have no problem with doing a call like

 https://example.com/cms/wp-json/pages?filter[tag]=L1

but I am unsure of how to call the API if I want to filter on posts that contain the tag L1 AND L2 AND L3, etc....

7
  • 1
    hmmmm, pages usually don't have tags Mar 4, 2015 at 14:43
  • @MarkKaplun , or using /posts? ... the content seems to be spread between posts & pages. But I'm just trying to figure this tag issue out.
    – ndyr
    Mar 4, 2015 at 15:37
  • @MarkKaplun is right, but for posts AFAIK posts?filter[tag]=L1 should work. Besides, if you are querying for pages, shouldn't it be something like posts?type[]=page&filter[tag]=L1. You probably should give the WP API Documentation another read. I've recently found an interesting article series about the WP API, maybe that will help you too. Mar 4, 2015 at 15:38
  • BTW look into tag__and for querying posts tagged with multiple specific tags. Mar 4, 2015 at 16:10
  • @MarkKaplun Tags aren't exposed for pages by default, but it's not hard to add them, or any custom taxonomy, to any post type. The WP API should support any mix of post types and custom taxonomies, IMO. It looks like the proper syntax for AND is /posts?type[]=page&tag[]=tag1&tag[]=tag2 Not sure how to do an OR query, though. Mar 10, 2015 at 20:30

1 Answer 1

3

I ran into the same problem but for posts. I found how to OR or AND tags together here: https://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters

To get entries that have both L1 AND L2 AND L3 ; use plus (+)

https://example.com/cms/wp-json/pages?filter[tag]=L1+L2+L3

In case somebody else comes along and wants to OR terms together I'll save you the trouble: To get entries that have either L1 OR L2 OR L3 ; use comma (,)

https://example.com/cms/wp-json/pages?filter[tag]=L1,L2,L3

I have only used this with posts but it should work for pages too.

1
  • FYI for anyone looking at this in the future, I don't think this is valid anymore. After moving the plugin code to the WP Core, it looks like you'll need a query parameter tag with the tag ID's now. Apr 24, 2017 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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