Why is per_page not working with categories in WP API?

Pretty much all the URLs you are using are invalid in some way: http://foobar.com/wp-json/wp/v2/posts/categories_name=hello categories_name is not a valid argument for listing posts, and even if it was you are missing the ? part of the query string. http://foobar.com/wp-json/wp/v2/posts/categories_name=hello?per_page=‌​1 This one is also missing the ?. Query parameters on a URL, which the API uses … Read more

WordPress REST Create Post of Custom Type

Make sure your post type is shown in the REST API. $args = array( //* Use whatever other args you want ‘show_in_rest’ => true, ‘rest_base’ => ‘myslug’, ‘rest_controller_class’ => ‘WP_REST_Posts_Controller’, ); register_post_type( ‘myslug’, $args ); The endpoint to create a post would then be http://example.com/wp-json/wp/v2/myslug. Edit: The above is all that’s needed for a custom … Read more

How can I get users email (and additional data) from the rest API?

To add the user’s email address in the REST API response, register an additional field to the user object and add the email address: register_rest_field( ‘user’, ‘user_email’, [ ‘get_callback’ => static function (array $user): string { return get_userdata($user[‘id’])->user_email; }, ] ); Please note, however, that this is strongly discouraged because anyone can then see the … Read more

REST API endpoint for elasticpress autosuggest

After some inspecting the WP_REST_Request, it turned out, that the get_body() method was the one I’m looking for. Anyhow, this is what I ended up with: add_action( ‘rest_api_init’, function() { register_rest_route( ‘ep’, ‘/as/’, [ ‘methods’ => \WP_REST_Server::CREATABLE, ‘callback’ => ‘ep_autosuggest’, ] ); } ); function ep_autosuggest( WP_REST_Request $data ) { // Elasticsearch PHP Client $client … Read more

REST API, get user role?

This is totally possible by registering your own rest field into the response. Here’s some documentation on modifying response data. https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/ Here’s how to add roles to the endpoint: function get_user_roles($object, $field_name, $request) { return get_userdata($object[‘id’])->roles; } add_action(‘rest_api_init’, function() { register_rest_field(‘user’, ‘roles’, array( ‘get_callback’ => ‘get_user_roles’, ‘update_callback’ => null, ‘schema’ => array( ‘type’ => ‘array’ … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)