Skip to content

Instantly share code, notes, and snippets.

@rpasillas
Last active November 10, 2016 02:29
Show Gist options
  • Save rpasillas/8c4516a866e4de138ca35d3b8b0f4baf to your computer and use it in GitHub Desktop.
Save rpasillas/8c4516a866e4de138ca35d3b8b0f4baf to your computer and use it in GitHub Desktop.
<?php
//--REST ROUTE FOR CUSTOM POST TYPE EVENTS
//--THIS IS WORKING
add_action( 'rest_api_init', function(){
register_rest_route( 'event-search/v1', '/search/', array(
'methods' => 'GET',
'callback' => 'getResults'
));
});
//--REGISTER THE START DATE META TO A EVENT REST REQUEST
//--THIS IS NOT WORKING
//--WHEN I SWITCH THIS FROM 'EVENT' TO 'POST' I GET THE METAS BACK IN THE REST REQUEST
//--DOES NOT RETURN ANYTHING AT ALL WHEN SWITCHED TO MY CPT.
add_action( 'rest_api_init', function(){
register_rest_field( 'event', 'event_start_date', array(
'get_callback' => 'getStartDate',
'update_callback' => null,
'schema' => null
));
});
//--GET CUSTOM POST TYPE META
function getStartDate( $object, $field_name, $request ){
return get_post_meta( $object['id'], 'banner_image', true );
}
//--HANDLE THE REST REQUEST
//--GRAB ALL POSTS IN THE EVENTS POST TYPES
//--WITH THE SUBMITTED CATEGORY(TAXONOMY), LOCATION(TAXONOMY) AND DATE(META FIELD GENERATED THROUGH PODS PLUGIN)
//--THIS IS 100% WORKING
//--PERHAPS THIS QUERY IS PREVENTING THE META (LINE 12) FROM SHOWING?
function getResults( WP_REST_Request $request ){
$category = $request->get_param( 'cat' );
$location = $request->get_param( 'loc' );
$date = $request->get_param( 'date' );
$page = $request->get_param( 'page' );
$posts_per_page = 2;
if( $page <= 0){
$page = 1;
}
$query_params = array (
'cache_results' => true,
'update_post_meta_cache' => true,
'update_post_term_cache' => true,
'post_status' => 'publish',
'post_type' => 'event',
'ignore_sticky_posts' => true,
'posts_per_page' => $posts_per_page,
'paged' => $page,
'meta_query' =>
array (
0 =>
array (
'key' => 'event_start_date',
'value' => $date,
'compare' => '>=',
'type' => 'DATE',
),
), //event_start_date
array (
0 => array (
0 => array (
'taxonomy' => 'events_category',
'field' => 'name',
'terms' => array ( 0 => $category, ),
'operator' => 'IN',
), //events_category
1 => array (
'taxonomy' => 'events_locations',
'field' => 'name',
'terms' => array ( 0 => $location, ),
'operator' => 'IN',
), //events_locations
'relation' => 'AND',
),
), //array with tax params
);
$query = new WP_Query();
$results = $query->query( $query_params );
$data = array();
if( !empty( $results ) ){
foreach( $results as $post ){
$data[] = $post;
}
return $data;
}else{
return new WP_Error( 'te_no_event_posts', 'No Events', array( 'status' => 404 ));
}
}
@jacobarriola
Copy link

Is this v1 or v2 REST API?

@jacobarriola
Copy link

And you're certain that the registered cpt slug = event? As well as certain that the post_meta key = event_start_date?

@jacobarriola
Copy link

https://gist.github.com/rpasillas/8c4516a866e4de138ca35d3b8b0f4baf#file-event-rest-route-php-L25

shouldn't the field be event_start_date instead of banner_image?

@rpasillas
Copy link
Author

I don't know how i missed your replies @jacobarriola.

  1. REST API V2.
  2. Certain on both accounts
  3. You're correct, i updated it to banner_image for the sake of this gist, however, when it was event_start_date it was behaving the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment