2

Is it possible to query the API directly using a tag slug rather than a tag ID? Or do you have to query the main tags endpoint to find out its ID?

For reference, you can fetch a tag this way:

GET /wp/v2/tags/

https://developer.wordpress.org/rest-api/reference/tags/#retrieve-a-tag

Does core support a method that would allow me to use a slug instead of an ID? Or is there a workaround/filter addition that might add this?

1

I was looking at this before... Here is what I found (about 7 months old from today 3/21/2018.)

The “correct” way to do this with the REST API is to get the IDs of each of those tags, then make the request using the ?tags= parameter: assuming “clicks” has ID 1, and “passes” has ID 2, that would look like this

http://www.example.com/wp-json/wp/v2/posts?tags[]=1&tags[]=2

Using slugs for querying taxonomy terms is an issue, but slug-based queries were deliberately left out of core because they are more prone to change than taxonomy term IDs. To demonstrate the issue, if you renamed a slug and made a request with an outdated slug, your request will fail; but if you use the ID, changing the slug will not break your query.

You can get the ID of a slug by querying for e.g. /wp/v2/tags?slug=clicks

  • Most of this answer isn't relevant to the question, but the last sentence on its own is literally the canonical answer – Tom J Nowell Mar 21 '18 at 23:36
  • Thanks Tom, I've been docked before for not providing enough information...just trying to give back to the group. – rudtek Mar 22 '18 at 19:48

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.