How do I Filter Custom Post Type by Custom Taxonomy in the newest WordPress RESTful API?

We can check out the /wp/v2/clock endpoint’s arguments here:

https://myapp.dev/wp-json/wp/v2/

If we add the 'show_in_rest' => true, argument, when registering the custom clock_category taxonomy, it will add a support for this GET/POST argument:

clock_category: {
    required: false,
    default: [ ],
    description: "Limit result set to all items that have the 
        specified term assigned in the clock_category taxonomy.",
    type: "array",
    items: {
        type: "integer"
    }
},

and this GET argument:

clock_category_exclude: {
    required: false,
    default: [ ],
    description: "Limit result set to all items except those that have the 
        specified term assigned in the clock_category taxonomy.",
    type: "array",
    items: {
        type: "integer"
    }
}

It will also add the /wp/v2/clock_category endpoint.

So to filter the clock posts by a single clock category ID:

https://myapp.dev/wp-json/wp/v2/clock?clock_category=10

or by multiple clock category IDs:

https://myapp.dev/wp-json/wp/v2/clock?clock_category=10,11,12

To exclude from a single category ID:

https://myapp.dev/wp-json/wp/v2/clock?clock_category_exclude=13

or exclude from multiple clock category IDs:

https://myapp.dev/wp-json/wp/v2/clock?clock_category_exclude=13,14

Leave a Comment