Skip to content

Instantly share code, notes, and snippets.

@LeMiira
Created August 16, 2021 10:16
Show Gist options
  • Save LeMiira/1609e4936d0552d1c79b0a64036a4c0e to your computer and use it in GitHub Desktop.
Save LeMiira/1609e4936d0552d1c79b0a64036a4c0e to your computer and use it in GitHub Desktop.
Customize rest API Wordpress
<?php
add_action(
'rest_api_init',
function () {
register_rest_route('emin', '/city', array(
'methods' => 'GET',
'callback' => 'cityEndpoint',
));
}
);
function cityEndpoint(WP_REST_Request $request) {
$searchedDistance =$request['filter_distance'] ?: 15;
$postsPerPage = $request['per_page'] ?: 3;
$citySearched = $request['city'];
$args = [
'posts_per_page' => $postsPerPage,
'post_type' => 'ag_location',
'paged' => $page,
's' => $searchKeyword,
];
global $wpdb;
//
//
$querySQL = "SELECT * FROM `uk_towns` WHERE ( `type` = 'city' OR `type` = 'town' OR `type` = 'town' ) AND `name` LIKE '" . $citySearched . "%' ";
$results = $wpdb->get_results($querySQL);
$i = 0;
$data = array();
foreach ($results as $result) :
$data[$i]['name'] = $result->name;
$data[$i]['type'] = $result->type;
$data[$i]['latitude'] = $result->latitude;
$data[$i]['longitude'] = $result->longitude;
$i++;
endforeach;
return $data;
//var_dump(isset($_COOKIE['storedsearch']));
if ($the_query->have_posts()) :
// foreach($posts as $post){
while ($the_query->have_posts()) : $the_query->the_post();
$id = get_the_ID();
// var_dump('id--->'.$post);
$lat = get_field('location_latitude', $id);
$long = get_field('location_longitude', $id);
$distance = (int) distance($lat, $long, $searchedLat, $searchedLong, "M");
// if($distance>=$searchedDistance){
// continue;
// }
// $totalFound++;
$distanceKM = (int) distance($lat, $long, $searchedLat, $searchedLong, "K");
$obj['totalresult'] = $totalPosts;
$obj['totalpages'] = $totalpages; //round($totalPosts/$postsPerPage);
$obj['id'] = $id;
$obj['title'] = get_the_title($id);
$providerID = get_field('attached_provider_id', $id);
$obj['specialist_service'] = get_the_terms($providerID, 'specialist_service');
$obj['distance_zero'] = (int) get_field('distance_to_zero', $id);
$distanceObj = get_the_terms($post, 'distance_zero');
$obj['distance_miles'] = $distance;
$obj['distance_kilometers'] = $distanceKM;
$obj['searched_distance_zero'] = $myZeroDistance;
$obj['distance_zero_name'] = (int) $distanceObj[0]->name;
$obj['distance_zero_id'] = $distanceObj[0]->term_id;
$obj['post_date'] = get_the_date($id);
$obj['date'] = get_the_date($id);
//$obj['date_gmt'] = get_gmt_from_date();
$obj['link'] = get_permalink($id);
$obj['service_category'] = get_the_terms($id, 'ag_primary_inspection_cat');
$obj['cqc_location'] = get_the_terms($id, 'cqc_location');
$obj['brand'] = get_the_terms($id, 'ag_brand');
$obj['staff'] = get_the_terms($id, 'ag_staff');
$obj['region'] = get_the_terms($id, 'ag_regions');
$obj['city'] = get_the_terms($id, 'ag_cities');
$obj['county'] = get_the_terms($id, 'ag_county');
$obj['type_sector'] = get_the_terms($id, 'ag_type_sector');
$obj['inspection_directorate'] = get_the_terms($id, 'ag_inspection_directorate');
$obj['ownership_type'] = get_the_terms($id, 'ag_ownership_type');
$obj['local_authority'] = get_the_terms($id, 'ag_local_authority');
$obj['parliamentary_constituency'] = get_the_terms($id, 'parliamentary_constituency');
$obj['onspd_ccg'] = get_the_terms($id, 'location_onspd_ccg');
$obj['thumbnail'] = get_field('thumbnail', $id);
$obj['locationId'] = get_field('locationId', $id);
$obj['care_home'] = get_field('care_home', $id);
$obj['location_phone_number'] = get_field('location_phone_number', $id);
$obj['location_website'] = get_field('location_website', $id);
$obj['attached_provider'] = get_field('attached_provider', $id);
$obj['attached_provider_id'] = $providerID;
$obj['uprn_id'] = get_field('location_uprn_id', $id);
$obj['latitude'] = $lat;
$obj['longitude'] = $long;
$obj['paf_id'] = get_field('location_paf_id', $id);
$obj['ods_code'] = get_field('location_ods_code', $id);
$obj['postal_code'] = get_field('location_postal_code', $id);
$obj['street_address'] = get_field('location_street_address', $id);
$obj['address_line_2'] = get_field('location_address_line_2', $id);
$obj['hsca_start_date'] = get_field('location_hsca_start_date', $id);
$obj['inherited_rating'] = get_field('inherited_rating', $id);
$obj['latest_overall_rating'] = get_field('location_latest_overall_rating', $id);
$obj['care_homes_beds'] = get_field('care_homes_beds', $id);
$obj['keyword'] = $request['keyword'];
//
// $temp[$i]['id'] = $post->ID;
// $temp[$i]['latitude'] = $lat;
// $temp[$i]['longitude'] = $long;
$data[$i] = $obj;
// var_dump($data,$i);
$i++;
//};
endwhile;
;
// session_start();
// $_SESSION["storedsearch"] = serialize($data);
// setcookie('storedsearch', serialize($data), time()+3600);
endif;
//sort data based on miles
if ($sortBy) {
var_dump($data);
}
usort($data, "milessort");
if (count($data) > 0) {
$data[0]['mytotal'] = count($data);
}
wp_reset_postdata();
// $data=fixPerPage($data,3,1);
return $data;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment