Last active
November 10, 2016 02:29
-
-
Save rpasillas/8c4516a866e4de138ca35d3b8b0f4baf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 )); | |
} | |
} |
https://gist.github.com/rpasillas/8c4516a866e4de138ca35d3b8b0f4baf#file-event-rest-route-php-L25
shouldn't the field be event_start_date
instead of banner_image
?
I don't know how i missed your replies @jacobarriola.
- REST API V2.
- Certain on both accounts
- 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
And you're certain that the registered cpt slug =
event
? As well as certain that the post_meta key =event_start_date
?