DescriptionDescription
This filter is run after querying posts, terms or users in the Fetch REST request route.
It allows to format results returned by the query.
ArgumentsArguments
Argument | Type | Description |
$posts | array | Holds posts from WP_Query |
$query | WP_Query | The WP_Query instance |
$params | array | Holds requested parameters |
ExampleExample
PHP
functions.php
function prefix_rest_api_results( $posts, $query, $params ) {
$request = new WP_REST_Request();
$controller = new WP_REST_Posts_Controller( 'post' );
return array_map(
function( $post ) use ( $controller, $request ) {
$data = $controller->prepare_item_for_response( $post, $request );
$post = $controller->prepare_response_for_collection( $data );
return $post;
},
$posts
);
};
add_filter( 'wp_grid_builder/rest_api/results', 'prefix_rest_api_results', 10, 3 );