Skip to content

Instantly share code, notes, and snippets.

@rpasillas
Last active November 10, 2016 02:29
Show Gist options
  • Select an option

  • Save rpasillas/8c4516a866e4de138ca35d3b8b0f4baf to your computer and use it in GitHub Desktop.

Select an option

Save rpasillas/8c4516a866e4de138ca35d3b8b0f4baf to your computer and use it in GitHub Desktop.

Revisions

  1. rpasillas revised this gist Nov 3, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions event-rest-route.php
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,8 @@

    //--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',
  2. rpasillas created this gist Nov 3, 2016.
    95 changes: 95 additions & 0 deletions event-rest-route.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,95 @@
    <?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
    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 ));
    }
    }