Get post count in wp rest API v2 and get all categories

The WP Rest API sends the total count(found_posts) property from WP_Query. in a header called X-WP-Total. FOR POSTS: you can make a call to posts endpoint of the REST API http://demo.wp-api.org/wp-json/wp/v2/posts The value for posts count is returned in the header as X-WP-Total. Below is a sample response from the hosted demo Access-Control-Allow-Headers:Authorization, Content-Type Access-Control-Expose-Headers:X-WP-Total, … Read more

REST API purpose?

At its current state, it is a badly engineered feature that do not have any real advantage for a competent developer. The basic idea, as it stands at the time this answer is written, is to expose WordPress core functionality as JSON REST API. This will enable decoupling of the WordPress “business” logic from the … Read more

How to do a meta query using REST-API in WordPress 4.7+?

You can write your own REST handler for custom queries if you want. In your case, the query can done by doing so: // Register a REST route add_action( ‘rest_api_init’, function () { //Path to meta query route register_rest_route( ‘tchernitchenko/v2’, ‘/my_meta_query/’, array( ‘methods’ => ‘GET’, ‘callback’ => ‘custom_meta_query’ ) ); }); // Do the actual … Read more

With Rest V2 (WP4.7) how does one restrict certain RESTFUL verbs?

I have gone through the source, and from what I can see, there aren’t any hooks/filters to tap into changing permissions. My understanding is that this was an intentional design decision. While the REST API was built to be extensible, it is not recommended to modify core endpoints in the way you are asking. There … Read more

WordPress Rest API custom endpoint optional param

You should put the named parameters of the route regex into an optional capturing group: register_rest_route( ‘api’, ‘/animals(?:/(?P<id>\d+))?’, [ ‘methods’ => WP_REST_Server::READABLE, ‘callback’ => ‘get_animals’, ‘args’ => [ ‘id’ ], ] ); The second parameter is simply a regex, thus you can use normal regex logic to make it more complex