18

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

thomers
  • 2,603
  • 4
  • 29
  • 50

5 Answers5

24

Found it, somewhat [hidden][1]

Using the category name: /posts?categories=1

Try this one.

Munim
  • 25
  • 1
  • 5
thomers
  • 2,603
  • 4
  • 29
  • 50
  • 1
    Hmm, actually it's not *that* hidden in the docs: http://wp-api.org/#posts_retrieve-posts_input – thomers Feb 18 '15 at 12:16
  • 1
    The link in your comment appears to not be correct anymore (perhaps it was changed on a version upgrade?). Anyways, is it possible to do a negative filter, such as all posts that don't have a `category_name` that is `MyCategory`? – The Unknown Dev Oct 18 '16 at 15:24
  • 15
    Changed in v2 `&categories=1` – Tom Woodward May 29 '17 at 15:58
  • 4
    It looks like wordpress removed filters from API since Wordpress 4.7. @TomWoodward's answer works. – geochanto Jan 30 '19 at 20:28
13

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"

Matteus Barbosa
  • 2,409
  • 20
  • 21
5

For category name, two filters should be added like this:

add_filter( "rest_post_query", function( $args, $request){
                if ( isset( $request['category_name']) && !empty($request['category_name'] ) ) {
                    $args['category_name'] = $request['category_name'];
                }
                return $args;
            }, 10, 2);


add_filter( "rest_post_collection_params", function($query_params, $post_type){
                $query_params[ 'category_name' ] = array(           
                    'description' => __( 'Category name.' ),
                    'type'        => 'string',
                    'readonly'    => true,
                );
                return $query_params;
            }, 10, 2);
Amjad Saeed
  • 96
  • 1
  • 2
  • This still works, I believe i seen this on the github issue as well nevertheless ty. – Spade May 23 '17 at 18:30
  • Thanks! For the record, I noticed for this to work, the category_name query parameter must be BEFORE the _fields parameter. – S. Roose Apr 09 '21 at 10:43
4

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

Faiz Ahmad Dae
  • 5,653
  • 2
  • 14
  • 19
-4

This example url worked for me... https://yourdomain.com/?rest_route=/wp/v2/posts&categories=99

ja de
  • 40
  • 6
  • 4
    What does this post add to this question, that the other answers don't? – Blue Jan 29 '18 at 21:11
  • You don't need to add all that code above - you can just access raw data via the location bar. – ja de Jan 30 '18 at 14:23
  • 2
    [This answer doesn't have any code](https://stackoverflow.com/a/28540217/4875631), and pretty much sums up this post. – Blue Jan 30 '18 at 14:27