Add Products to Woocommerce through WP API

After a lot of searching and banging my head everywhere I have successfully implemented to add WooCommerce products through Rest API. Here is my complete working code for reference :- $api_response = wp_remote_get(‘www.myapi.com’); $body = wp_remote_retrieve_body($api_response); /****** API not ready yet…working on it *******/ $data = [ ‘type’ => ‘variable’, ‘description’ => ‘Trying it out … Read more

WP API to fetch all the media/posts records if count is more than 100

Add a page number query to your request URL, and continue querying next page numbers until you get an empty result: <your_wordpress_install_base_url>/wp-json/wp/v2/media/?per_page=100&page=2 Mark’s suggestion to consider requesting less than 100 per page may be a good one. So for example you may want to do your query this way: <your_wordpress_install_base_url>/wp-json/wp/v2/media/?per_page=25&page=3 Note also that (for me … Read more

Get the Category Name instead of ID from WP-API

This code will add categories_names field to wp rest api response: function wpse_287931_register_categories_names_field() { register_rest_field( ‘project’, ‘categories_names’, array( ‘get_callback’ => ‘wpse_287931_get_categories_names’, ‘update_callback’ => null, ‘schema’ => null, ) ); } add_action( ‘rest_api_init’, ‘wpse_287931_register_categories_names_field’ ); function wpse_287931_get_categories_names( $object, $field_name, $request ) { $formatted_categories = array(); $categories = get_the_category( $object[‘id’] ); foreach ($categories as $category) { $formatted_categories[] … Read more

WordPress wp-json API – Custom Post Type returns 403

In version 1.1 of the JSON REST API custom post type endpoints have to be registered manually. See: http://wp-api.org/guides/extending.html#registering-your-endpoints In version 2.0 we register endpoints for any custom post types registered with the show_in_rest property set to true. Documentation on how to do this: http://v2.wp-api.org/extending/custom-content-types/#registering-a-custom-post-type-with-rest-api-support

Hiding API routes list

You can use the filter hook ‘rest_index’ : add_filter(‘rest_index’, function ($response) { $data = $response->get_data(); $data[‘namespaces’] = []; $data[‘routes’] = []; return $data; }, 9999, 1); It is possible to remove your route from $data[‘namespaces’] and $data[‘routes’]

WP-API: how do I allow authenticated clients only?

This is how I did it, but I feel it could be better. For one thing, this results in HTTP 500… 403 would be preferable add_filter( ‘json_authentication_errors’, function( $authenticated ) { if( !$authenticated ) { return new WP_Error(‘Access Denied’); } }, 99 ); (I understand this’ll work for Basic Auth too)

How to filter or search the posts using postmeta tables custom meta fields with wordpress REST API

You will need to add custom query vars: add_filter(‘rest_query_vars’, ‘wpse225850_add_rest_query_vars’); function wpse225850_add_rest_query_vars($query_vars) { $query_vars = array_merge( $query_vars, array(‘meta_key’, ‘meta_value’, ‘meta_compare’) ); return $query_vars; } Now, get your posts at example.com/wp-json/wp/v2/posts?filter[meta_key]=property_featured&filter[meta_value]=1. You can follow this ticket for more info.

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