open kadamwhite/wordpress-rest-api#95

Get taxonomy by slug

PanMan

In the docs (frontpage) it says:

wp.taxonomies().taxonomy( 'taxonomy_name' ).term( termIdentifier ): get the term with slug or ID termIdentifier from the taxonomy taxonomy_name

This works with the ID, but not with the slug :(. Is this something that changed?

HOST/wp-json/taxonomies/post_tag/terms/42 works HOST/wp-json/taxonomies/post_tag/terms/sociaal-ondernemerschap doesn't

kadamwhite

This may just have been a mistake on my part, I can't find a prior version of the API that did support this—I'll look into it today

kadamwhite

This is definitely not supported at present, thank you for the catch. I believe I will add a .slug() method to the Taxonomies query builder to allow terms to be provided by slug; however, this depends on WP-API/WP-API#347, because the version of the API in the plugin directory does not support filtering taxonomy collection endpoints. I am not sure how to proceed in releasing this feature given that it is not in the officially-released version of the plugin.

What version are you using? You can try this query; in the latest "master" version of the plugin, it will return an array with a single element:

HOST/wp-json/taxonomies/post_tag/terms?filter[slug]=sociaal-ondernemerschap
kadamwhite

c.f. update: the actual PR that landed this change was WP-API/WP-API#401, not 347

PanMan

The filter by slug is what I had in my code (and which I think worked last week?), but now it returns all items, and doesn't filter.

kadamwhite

What version of the WP-API plugin are you using?

PanMan

The latest stable from wordpress.org, 1.1.1

kadamwhite

That makes sense. Filtering for taxonomy collections is not included in 1.1.1, so there won't be any way to get at a specific term until a new plugin version is released; I will add the .slug convenience method at that point.

The best I can offer is to filter down the returned collection:

wp.tags().then(function(tags) {
  return _.findWhere(tags, {
    slug: 'term-slug'
  });
});

It is not super efficient to request all terms each time, so you may want to request all of them and cache that collection locally in a variable or an LRU cache to speed up the retrieval.

PanMan

I think I'll update the WP-API on the server. Thnx for your input!

kadamwhite

In the intervening year, this has been broken again by the v2 beta upgrades: WP-API/WP-API#924 identifies that filter is not currently supported on the /categories or /tags endpoints.

WP-API/WP-API#2065 has been opened to identify whether this will ever be reinstated; in the meantime, #126 will add a demonstration of using the ?search parameter to slightly more efficiently retrieve a specific term by slug (basically the same "get them all and search for the one you want in the response" approach detailed above, but with less overhead due to the narrowing effect of ?search=the-slug).