Wp Rest Api Custom Endpoint for page subpages

It works.. function list_subpages() { $data = array(); $request = array(); $id = 151; $subpages = get_pages( array( ‘child_of’ => $id, ‘sort_column’ => ‘menu_order’ ) ); if ( empty( $subpages ) ) { return null; } foreach ($subpages as $p) { $data[‘id’] = $p->ID; $data[‘title’] = $p->post_title; $data[‘img’] = wp_get_attachment_url( get_post_thumbnail_id($p->ID) ); $request[] = $data; … Read more

WP Rest API convert permalink to post ID for fetch

The core rewrite API does offer the function url_to_postid() (as mentioned by @stephen-sabatini) which can find a post ID from a URL. However it seems less than ideal to have to make a request up front just to determine the post ID… The REST API does not natively offer the ability to query by date … Read more

rest_post_query on multiple post types?

Define the callback as a named function and hook it separately for each post type. function wpse_299908_order_rest_query( $args ) { $args[‘orderby’] = ‘menu_order’; $args[‘order’] = ‘ASC’; return $args; } add_filter( ‘rest_post_query’, ‘wpse_299908_order_rest_query’ ); add_filter( ‘rest_page_query’, ‘wpse_299908_order_rest_query’ ); There’s no filter that automatically applies to all endpoints, likely for the same reason that you can’t query … Read more

WordPress Gutenberg get page template value when post updated?

So I found a solution. There are 4 hooks that can be used to accomplish this depending on the exact needs. The hooks are from wp-includes/meta.php in functions update_metadata() and add_metadata(). Hooks: update_postmeta updated_postmeta add_post_meta added_post_meta These are called at different states and from the names it is pretty self self explanatory. add_post_meta and update_postmeta … Read more

Filter post content in REST API

It looks to me like you’ve written $data->$data[…] where you mean to have $data->data[…]. The “object to string” conversion error is probably due to the second $data, where PHP expects an object’s property name but is getting another reference to the $data object.

Can I define multiple callback methods depending on the call method?

Check if your code looks like this because in the question you pass each method as separate function arguments (I have overlooked it earlier) add_action( ‘rest_api_init’, function () { register_rest_route(‘my-project/v1/’, ‘/form’, array( array(‘methods’ => ‘GET’, ‘callback’ => ‘GET_form’, ), array(‘methods’ => ‘POST’, ‘callback’ => ‘post_form’ ) ) ); }); As you can read in documentation: … Read more

Upload image to wordpress using REST API

You can upload images just like your normal PHP/Wordpress file uploads. Reference => wp_handle_upload $mimes = array( ‘bmp’ => ‘image/bmp’, ‘gif’ => ‘image/gif’, ‘jpe’ => ‘image/jpeg’, ‘jpeg’ => ‘image/jpeg’, ‘jpg’ => ‘image/jpeg’, ‘png’ => ‘image/png’, ‘tif’ => ‘image/tiff’, ‘tiff’ => ‘image/tiff’ ); $overrides = array( ‘mimes’ => $mimes, ‘test_form’ => false ); $upload = wp_handle_upload( … Read more

Trying to get an api request getting error 404

Try using a GET request instead of a POST request. The headers indicate they only allow origin from weedmaps.com in the POST request. The method in WordPress is wp_remote_get(). function call_for_api() { $url=”https://api-v2.weedmaps.com/api/v2/listings”; $response = wp_remote_get( $url, array( ‘timeout’ => 45, ‘redirection’ => 5, ‘httpversion’ => ‘1.0’, ‘blocking’ => true, ‘headers’ => array( ‘accept’ => … Read more

Unable to get the info of the user which doesn’t have created any post via REST API

This is by design and within WP_REST_Users_Controller::get_item_permissions_check() we e.g. have a non-zero check with count_user_posts( $user->ID, $types ). We note that count_user_posts() is filterable with the get_usernumposts filter so one could change 0 to 1, for the /wp/v2/users/\d+ route: add_filter( ‘rest_request_before_callbacks’, function( $response, $handler, $request ){ if ( WP_REST_Server::READABLE !== $request->get_method() ) { return $response; … Read more