admin管理员组文章数量:1291666
I have been meddling with REST API and I am stuck with this: How can I filter posts in both cat1 and cat2?
For now, ?categories[]=45&categories[]=50
returns in category ID 45 OR 50 - how can I get posts in 45 AND 50?
Thanks in advance.
I have been meddling with REST API and I am stuck with this: How can I filter posts in both cat1 and cat2?
For now, ?categories[]=45&categories[]=50
returns in category ID 45 OR 50 - how can I get posts in 45 AND 50?
Thanks in advance.
Share Improve this question edited Jan 6, 2017 at 9:47 Mark Kaplun 23.7k7 gold badges43 silver badges65 bronze badges asked Jan 6, 2017 at 9:39 Jesse P FrancisJesse P Francis 3291 gold badge2 silver badges6 bronze badges 2- Have you found a solution for this? – Corey Commented Feb 3, 2017 at 17:03
- @Corey: Nopes. Guess it's not possible,as of now (saw some other plugin support posts claiming that) I was using API for an app with Ionic, I loaded them separately and intersected instead. Leaving it open, in case my learning is incomplete. Did read somewhere where Plugin is edited (not at all recommended) to achieve it. – Jesse P Francis Commented Feb 5, 2017 at 15:17
8 Answers
Reset to default 15Multiple categories can be separated by comma like below
http://example/wp-json/wp/v2/posts?categories=20,30
hope this helps
@Jesse see: WP-API/WP-API#2990
Since WP 4.7, filter
has been removed from WP-API.
You need to use this plugin: https://github/WP-API/rest-filter
There doesn't seem to be a way to do this in the current version of the API. Without using a plugin, it can be achieved with a custom endpoint or by using the rest query filter function for the specific post type.
Here's a rough filter function that adds a parameter called cat_relation
:
add_filter( 'rest_post_query', function( $args, $request ) {
if($request['cat_relation'] == 'AND') {
$args['category__and'] = $request['categories'];
}
return $args;
}, 10, 2);
So an example request URL would be:
http://example/wp-json/wp/v2/posts?categories=17,8&cat_relation=AND
Install the filter plugin Austin mentioned (https://github/WP-API/rest-filter) and try ?filter[categories]=cat_one_slug%2Bcat_two_slug
.
I found out that %2B
is the code equivalent of the +
symbol.
Normally we would use +
for the AND operator but unfortunately it gets converted into a space so use %2B
instead.
Filtering posts based on multiple custom taxonomy terms based on logical "AND", "OR" condition.
Here is what you need to do in the newer versions of Wordpress (v5 and above)
Lets say you have two custom taxonomies: sector and offerings.
Getting all posts that are tagged with taxonomy ID 51 "OR" 52 beloning to the taxonmy term "sector" "OR" Getting all posts that are tagged with taxonomy ID 57 "AND" 58 beloning to the taxonomy term "offering"
https://mywordpressbackend/wp-json/wp/v2/posts?sector=51+52&offering=57,58&tax_relation=OR
Getting all posts that are tagged with taxonomy ID 51 "OR" 52 beloning to the taxonmy term "sector" "AND" Getting all posts that are tagged with taxonomy ID 57 "AND" 58 beloning to the taxonomy term "offering"
https://mywordpressbackend/wp-json/wp/v2/posts?sector=51+52&offering=57,58&tax_relation=AND
You should be able to access multiple categories by using the following:
http://YOURSITE.DEV/wp-json/wp/v2/posts?categories=45+50
Hope that helps!
Cheers
This is what I did, It works fine.
http://example/wp-json/wp/v2/articles/?_embed&categories=1,2,3,4&per_page=30
Okay so it's going to say the categories number for each post when you fetch posts.
Then you can just filter by the category number like this:
const wpdata = await fetch(`http://example/wp-json/wp/v2/posts`);
const jsonresp = await wpdata.json()
const particularcategoryposts = jsonresp.filter(function(item){
return item.categories == "4";
});
P.S.
By default the WP API only returns 10 posts. Make sure to clarify that you'll need to fetch more than 10 posts if that is the case. 100 Posts is the max.
const wpdata = await fetch(`http://example/wp-json/wp/v2/posts/?per_page=100`);
本文标签: categoriesWP REST API filter by category 1 AND category 2
版权声明:本文标题:categories - WP REST API: filter by category 1 AND category 2 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741535990a2384028.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论