Skip to content

Instantly share code, notes, and snippets.

@simplethemes
Created October 13, 2015 22:49
Show Gist options
  • Save simplethemes/411ebd192e4fcd25781b to your computer and use it in GitHub Desktop.
Save simplethemes/411ebd192e4fcd25781b to your computer and use it in GitHub Desktop.
add_action('wp-routes/register_routes', function(){
with('/location-api', function(){
respond('GET', '/[i:id]', function($request){
$post = get_post($request->id);
// $post = get_posts(array('post_type' => 'location'));
header('Content-Type: application/json');
if ($post) {
$fields = array(
'post_id' => $post_id = $post->ID,
'title' => $post->post_title,
'url' => $post->guid,
'location_address' => $location_address = get_field('location_address',$post_id),
'location_map' => $location_map = get_field('location_map',$post_id),
'location_phone_number' => $location_phone_number = get_field('location_phone_number',$post_id),
'location_hours' => $location_hours = get_field('location_hours',$post_id),
'location_book_online_url' => $location_book_online_url = get_field('location_book_online_url',$post_id),
'content' => $post->post_content
);
// $address = get_field('location_address', $id);
echo json_encode($fields);
}
else {echo json_encode(array('error' => 'Not a valid post ID'));}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment