Using pre_get_posts, how to target the REST API, only?

Yes. So long as the code you’re running happens after parse_request – which pre_get_posts does. (See https://codex.wordpress.org/Plugin_API/Action_Reference#Actions_Run_During_a_Typical_Request) Then you will have a nice constant to use: REST_REQUEST Something like if( defined( ‘REST_REQUEST’ ) && REST_REQUEST ) { // …do RESTy things } will do the trick. See /wp-includes/rest-api.php

Exclude posts w/ specific post_meta on Rest API endpoint

Caveat From the REST API docs: Changing or removing data from core REST API endpoint responses can break plugins or WordPress core behavior, and should be avoided wherever possible. That said, there’s a rest_post_query filter (well, rest_{$this->post_type}_query) that will let you modify the WP_Query that’s grabbing your posts, so you should be able to add … Read more

WP REST API format response

Since you’re filtering with the rest_prepare_{post_type} filter, you could restrict it to the WP_REST_Posts_Controller::get_items() callback, with the rest_{post_type}_query filter: add_filter( ‘rest_post_query’, function( $args ) { add_filter( ‘rest_prepare_post’, ‘api_remove_extra_data’, 12, 3 ); return $args; } ); where the post type is post. Note that in general we always want to return the filter value and I … Read more

How can I cache WordPress Rest API Response

You should create a new instance from WP_REST_Response to set the Cache-Control value. <?php register_rest_route(‘wp/v2’, ‘/your_endpoint’, array( ‘methods’ => ‘GET’, ‘callback’ => ‘function_callback’, )); function function_callback($data) { $response = array( 1,2,3,4,5,6,7,8 ); $result = new WP_REST_Response($response, 200); // Set headers. $result->set_headers(array(‘Cache-Control’ => ‘max-age=3600’)); return $result; } Click here to get more info about directives.

WP REST api.wordpress.org discovery

https://api.wordpress.org is a custom PHP API and is not running WordPress. Thus, there’s no WP REST API that you could use for discovery. That URL has nothing to do with the REST API in core except for that redirect to the handbook. Unfortunately not all of the API endpoints are documented or either open sourced, … Read more

woocommerce product custom field

You should ad the custom field to product API response, try this (untested) add_filter( ‘woocommerce_rest_prepare_product’, ‘custom_products_api_data’, 90, 2 ); function custom_products_api_data( $response, $post ) { // retrieve a custom field and add it to API response $response->data[‘purchasing_cost’] = get_post_meta( $post->ID, ‘purchasing_cost’, true ); return $response; }

Can we assume that /wp-json/ will always be /wp-json/ under any circumstances when creating custom REST routes?

Sure they can! Actually the structure you used is assuming that url rewrites are on. Which may be or may be not. To assure the right endpoint, you must use get_rest_url function: https://developer.wordpress.org/reference/functions/get_rest_url/ So in your case, this would be: ‘get_rest_url’ => get_rest_url(null, ‘/block_help/v1/block_identi…’)

WordPress REST API – Modify JSON before importing

Using the rest_pre_dispatch hook is probably the most straightforward way to go, but you need to be careful about putting your new request data back into the WP_REST_Request object properly. You were reassigning the WP_REST_Request object to an array of request data. Those changes are not persisted because the parameter is not passed by reference. … Read more