Should be easy, but didnt find it in the WP-API docs.
Now, if you want to display all your posts from a specific category on a separate page, WordPress already takes care of this for you. To find the category page, you simply need to go to Posts » Categories » View page and click on the 'View' link below a category.
Get an API key for the API. Create a Plugin for adding a widget to your WordPress site. Customize your plugin with your API key and specific information you want to display in the widget. Use the WordPress Admin Area to place the widget on your site where you want it within your theme.
This question is a duplicate from this other question here from the forum
http://example.com/wp-json/wp/v2/posts?categories=20,30
The above will return posts from category 20 OR category 30
I've tested with custom post types and it also works perfectly
The response and credits go to "Manish Jung Thapa"
This code is working for me
Add to your function.php
function rest_filter_by_custom_taxonomy( $args, $request ) {
if ( isset($request['category_slug']) )
{
$category_slug = sanitize_text_field($request['category_slug']);
$args['tax_query'] = [
[
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $category_slug,
]
];
}
return $args;
}
add_filter('rest_post_query', 'rest_filter_by_custom_taxonomy', 10, 3);
EX: /wp-json/wp/v2/posts?category_slug=news
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With