Query the REST API for a Tag by slug

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/

Tags

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 Answer
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

Leave a Comment