Created
September 23, 2017 04:35
-
-
Save deguchi/25834ffdfa6d8fa2ebee0248c56bbd0e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // Register a REST route | |
| add_action( 'rest_api_init', function () { | |
| //Path to meta query route | |
| register_rest_route( 'boardgame/v2', '/fedrated_query/', array( | |
| 'methods' => 'GET', | |
| 'callback' => 'custom_meta_query' | |
| ) ); | |
| }); | |
| // Do the actual query and return the data | |
| function custom_meta_query(){ | |
| if(isset($_GET['query'])) { | |
| $query = $_GET['query']; | |
| // Set the arguments based on our get parameters | |
| $args = Array( | |
| 'post_type' => 'post', | |
| 'posts_per_page' => 100, | |
| 'meta_query' => array( | |
| 'relation' => 'OR', | |
| array( | |
| 'key' => 'etitle', | |
| 'value' => $query, | |
| 'compare' => 'LIKE' | |
| ), | |
| ) | |
| ); | |
| // $args = array ( | |
| // array( | |
| // 'meta_key' => 'etitle', | |
| // 'meta_value' => 'catan', | |
| // 'meta_compare' => 'LIKE', | |
| // ), | |
| // // array( | |
| // // 'key' => $query[0]['key'], | |
| // // 'value' => $query[0]['value'], | |
| // // 'compare' => '=', | |
| // // ), | |
| // ); | |
| // Run a custom query | |
| $meta_query = new WP_Query($args); | |
| if($meta_query->have_posts()) { | |
| //Define and empty array | |
| $data = array(); | |
| // Store each post's title in the array | |
| while($meta_query->have_posts()) { | |
| $meta_query->the_post(); | |
| $data[] = get_the_title(); | |
| } | |
| // Return the data | |
| return $data; | |
| } else { | |
| // If there is no post | |
| return []; | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment