投稿タイプごとに 3 つの異なる HTTP リクエストを作成するのではなく、投稿タイプに基づいて 3 つのカスタム クエリを返すように、作成中のアプリのフロント ページ用のカスタム REST API エンドポイントを作成しましたが、方法がわかりません。表示する各投稿のカスタム フィールドを取得します。次にどこに行けばよいかわからない:
class Home_Custom_Route extends WP_REST_Controller {
/**
* Register the routes for the objects of the controller.
*/
public function my_register_routes() {
$version = 'v2';
$namespace = 'wp/' . $version;
$base = 'home';
register_rest_route( $namespace, '/' . $base, array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => array(
),
),
) );
register_rest_route( $namespace, '/' . $base . '/schema', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_public_item_schema' ),
) );
}
/**
* Get a collection of items
*
* @param WP_REST_Request $request Full data about the request.
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
$eventargs = array(
'post_type' => 'event',
'posts_per_page' => 3,
'meta_key' => 'wpcf-event-start',
'meta_value' => current_time( 'timestamp', 1 ),
'meta_compare' => '<=',
);
$main_events = new WP_Query( $eventargs );
$listingargs = array(
'post_type' => 'listings',
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC',
);
$main_listings = new WP_Query( $listingargs );
$ticketsargs = array(
'post_type' => 'product',
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'tickets',
)
),
);
$main_tickets = new WP_Query( $ticketsargs );
$data = array(
'events' => $main_events->posts,
'listings' => $main_listings->posts,
'tickets' => $main_tickets->posts,
);
return new WP_REST_Response( $data, 200 );
}
/**
* Get one item from the collection
*
* @param WP_REST_Request $request Full data about the request.
* @return WP_Error|WP_REST_Response
*/
public function get_item( $request ) {
//get parameters from request
$params = $request->get_params();
$item = array();//do a query, call another class, etc
$data = $this->prepare_item_for_response( $item, $request );
//return a response or error based on some conditional
if ( 1 == 1 ) {
return new WP_REST_Response( $data, 200 );
}else{
return new WP_Error( 'code', __( 'Couldnt find it', 'xxx' ) );
}
}
/**
* Check if a given request has access to get items
*
* @param WP_REST_Request $request Full data about the request.
* @return WP_Error|bool
*/
public function get_items_permissions_check( $request ) {
return true; //<--use to make readable by all
}
/**
* Check if a given request has access to get a specific item
*
* @param WP_REST_Request $request Full data about the request.
* @return WP_Error|bool
*/
public function get_item_permissions_check( $request ) {
return $this->get_items_permissions_check( $request );
}
/**
* Prepare the item for the REST response
*
* @param mixed $item WordPress representation of the item.
* @param WP_REST_Request $request Request object.
* @return mixed
*/
public function prepare_item_for_response( $item, $request ) {
/*pretty sure this is where custom fields are enabled, but not sure how to do that*/
}
}