Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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
You can’t perform that action at this time.