Wordpress - WP-API v2 Custom Endpoint Response Formatting

You can call the REST API methods to prepare your output in the same way that the plugin does by default, this will also allow any plugins to tie into the output as you have used the ACF plugin as seen in your example output.

The WP_REST_Posts_Controller class has the following in its get posts method

$posts_query = new WP_Query();
$query_result = $posts_query->query( $query_args );

$posts = array();
foreach ( $query_result as $post ) {
    if ( ! $this->check_read_permission( $post ) ) {
        continue;
    }

    $data = $this->prepare_item_for_response( $post, $request );
    $posts[] = $this->prepare_response_for_collection( $data );
}

So you could instantiate a new WP_REST_Posts_Controller instance and call the prepare_item_for_response and prepare_response_for_collection methods on your data to format it identically to the default endpoints.

Something similar to the following should work (untested)

function wp_json_offers_v2__posts($request) {
    // json-api params

    $parameters = $request->get_query_params();

    // default search args

    $args = array(
        'post_type'     => $parameters['type'],
        'numberposts'   => 9,
        'offset'        => $parameters['offset'],
        'post_not_in'       => $parameters['exclude'],
        'orderby'       => 'rand',
    );

    // run query

    $posts = get_posts($args);

    $controller = new WP_REST_Posts_Controller($parameters['type']);

    foreach ( $posts as $post ) {
       $data    = $controller->prepare_item_for_response( $post, $request );
       $posts[] = $controller->prepare_response_for_collection( $data );
    }

    // return results
    return new WP_REST_Response($posts, 200);
}

The accepted answer was helpful. Thanks. But prepare_item_for_response() method was deprecated since v.3.0, so, hope this snippet will help someone with a custom endpoint for WC Orders.

    // Parse all others params
    $args = wp_parse_args( $request->get_params(), $args );

    // Get orders
    $orders = wc_get_orders( $args );

    if ( ! empty( $orders ) && is_array( $orders ) ) {
        $controller = new WC_REST_Orders_Controller ();

        // The response data
        $orders_data = [];
        // We need prepare objects to the response
        foreach ( $orders as $order ) {
            if ( $order instanceof WC_Order ) {
                // Objects
                $prepared_object = $controller->prepare_object_for_response( $order, $request );
                $orders_data[]   = $controller->prepare_response_for_collection( $prepared_object );
            } else {
                // IDs
                $orders_data[] = (int) $order;
            }
        }

        // Return the response
        return new WP_REST_Response( $orders_data, 200 );

    }

Tags:

Php

Api

Wp Api