Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a list of posts for a category with WP-API?

Should be easy, but didnt find it in the WP-API docs.

like image 732
thomers Avatar asked Feb 16 '15 11:02

thomers


People also ask

How do I fetch category posts in WordPress?

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.

How do I get a WordPress API request?

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.


2 Answers

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"

like image 107
Matteus Barbosa Avatar answered Sep 18 '22 01:09

Matteus Barbosa


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

like image 32
Faiz Ahmad Dae Avatar answered Sep 20 '22 01:09

Faiz Ahmad Dae