WooCommerce – Create multiple product tags via rest api endpoint

Putting aside whether you’re making the API request from and to the same site or not, I assume that the $woocommerce is an instance of Automattic\WooCommerce\Client. And you can use the batch endpoint for product tags (/wp-json/wc/v3/products/tags/batch) to create/update/delete multiple (by default, up to 100) product tags at a time. Here’s an example for creating … Read more

WordPress REST API register_rest_route give a 500 error

Revised answer To other readers, another issue was the OP used the wrong route. So make sure to make the API request to the correct route, which is a combination of the first two parameters for register_rest_route() (namespace + route base). So with register_rest_route( ‘uheme/v1’, ‘/menu’ ), the route is uheme/v1/menu. Additionally: With permalinks enabled, … Read more

How to use apiFetch to get author information in Gutenberg properly?

Finally, I found out how to do it. Just call fetchApi in componentDidMount and then create a new state in your component and add the response to this state when the request is done: constructor(props) { super(props); this.state = { data: {}, }; } componentDidMount() { apiFetch( { path: ‘/wp/v2/users/1’ } ) .then(data => this.setState({ … Read more

How to get post with associated categories and tags names instead of ids with rest api?

When I copy/pasted your code I got an error message because enf_register_categories_names_field() wasn’t defined. When I cleaned up your code and wrapped the register_rest_field() in a function called “enf_register_categories_names_field” it worked as expected. function ag_filter_post_json($response, $post, $context) { $tags = wp_get_post_tags($post->ID); $response->data[‘tag_names’] = []; foreach ($tags as $tag) { $response->data[‘tag_names’][] = $tag->name; } return $response; … Read more

Custom REST endpoint not working to retrieve single posts (“rest_no_route”)

To get your expected result, you have to create a second endpoint. Your endpoint doesn’t know what to do with the last part of the path (the ID). register_rest_route(‘mytheme/v1/’, ‘my_cpt/(?P<id>[\d]+)’, array( ‘methods’ => WP_REST_SERVER::READABLE, ‘callback’ => ‘my_cpt_single_result’, )); Your CB function could be like this: function my_cpt_single_result( $data ) { $cpt_id = $data[‘id’]; // make … Read more

Cant register rest routs from class instance

There’s several things wrong here, none really related to OOP. Firstly, you seem to be attempting to register the REST routes on plugin activation. This is incorrect. Routes need to be registered for every request, so BusinessCustomersApiController->register_routes() needs to run on every request. You do this by hooking that function to the rest_api_init. The problem … Read more

WordPress REST Api get posts by ID

You’re getting that mismatched values because you’re using the wrong argument for the post ID in your $args array (i.e. the query args for WP_Query). And the correct argument is p (lowercase P) and not id: $args = [ // ‘id’ => $request[‘id’], // wrong argument name – ‘id’ ‘p’ => $request[‘id’], // and the … Read more