/rest_api/results

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

ArgumentTypeDescription
$postsarrayHolds posts from WP_Query
$queryWP_QueryThe WP_Query instance
$paramsarrayHolds 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 );