最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

WP REST API no longer supports filter param, so how do I get posts in a custom taxonomy?

programmeradmin2浏览0评论

All around the Interwebs I see advice from people who say that when you want to get posts in a custom taxonomy you should use the filter parameter, for example:

[genre]=fiction

This seems like a very handy parameter. But in v2 of the WP REST API it just doesn't work. When I created a WP Trac ticket to find out what was going on, @swissspidy responded that "the filter param has been removed on purpose" but that the documentation hasn't been updated yet. The change is discussed in Trac ticket 38378.

OK, fair enough, but could someone tell me how I should retrieve posts in a custom taxonomy now? I'm writing a plugin that depends on being able to do this.

For example, if I've created a non-hierarchical custom taxonomy instance and given it the value 1 for certain posts in a custom post type, how can I retrieve all the posts of that type and with instance=1?

If it's not possible via the REST API, is there a way to do it via the WordPress API on a Jetpack-enabled self-hosted site?

All around the Interwebs I see advice from people who say that when you want to get posts in a custom taxonomy you should use the filter parameter, for example:

https://example/wp-json/wp/v2/posts?filter[genre]=fiction

This seems like a very handy parameter. But in v2 of the WP REST API it just doesn't work. When I created a WP Trac ticket to find out what was going on, @swissspidy responded that "the filter param has been removed on purpose" but that the documentation hasn't been updated yet. The change is discussed in Trac ticket 38378.

OK, fair enough, but could someone tell me how I should retrieve posts in a custom taxonomy now? I'm writing a plugin that depends on being able to do this.

For example, if I've created a non-hierarchical custom taxonomy instance and given it the value 1 for certain posts in a custom post type, how can I retrieve all the posts of that type and with instance=1?

If it's not possible via the REST API, is there a way to do it via the WordPress API on a Jetpack-enabled self-hosted site?

Share Improve this question edited Dec 3, 2016 at 10:19 And Finally asked Dec 3, 2016 at 9:59 And FinallyAnd Finally 2971 gold badge3 silver badges16 bronze badges 5
  • Try this plugin github/bueltge/wp-rest-api-filter-items – bueltge Commented Dec 7, 2016 at 9:41
  • Thanks @bueltge, but it looks like that plugin is to filter what fields the API returns for each post? – And Finally Commented Dec 7, 2016 at 12:19
  • Currently it helps to filter posts, comments, taxonomies. If you need more, a enhancement in github/bueltge/wp-rest-api-filter-items/tree/master/inc/… is necessary. – bueltge Commented Dec 7, 2016 at 13:12
  • 1 Also in this context github/WP-API/rest-filter – bueltge Commented Dec 7, 2016 at 18:14
  • Ah thank you! That's the kind of filter I was talking about! If you want to post that as an answer I'll accept it. Presumably there were good reasons for removing the filter argument, so it may not be the best thing in every case to restore it, but that's an extremely handy plugin which points me towards how to achieve what I want to do. – And Finally Commented Dec 8, 2016 at 11:41
Add a comment  | 

2 Answers 2

Reset to default 4

Since WordPress 4.7 the filter argument for any post endpoint was removed. But if you need them, add them via plugin. The WP API Repo have a plugin 'Rest Filter' for this job, small and short.

The WordPress REST API documentation has a section on registering custom posts and taxonomies with the API. Essentially, you add 'show_in_rest' => true when registering the custom post type or custom taxonomy.

register_taxonomy('instance', ['post'], [
    <other args...>,
    'show_in_rest' => true,
]);

Once that's done, you can filter any object for which you registered that taxonomy ('post' in the above example) by that custom taxonomy by using its name as a query string parameter, e.g.:

https://example/wp-json/wp/v2/posts?instance=1

Note that the value of the taxonomy parameter ('1' in the above example) has to be the id of the taxonomy object, not it's name or slug. If you only know the slug, and not the id, you can first lookup the id by separately querying for it, because registering your custom taxonomy with the API also creates its own custom endpoint:

https://example/wp-json/wp/v2/instance?slug=foo-bar
发布评论

评论列表(0)

  1. 暂无评论