Hi @bvytis,
Here is the full code:
add_action('rest_api_init', function () {
register_rest_route( 'westland/v1', 'shops',array(
'methods' => 'GET',
'callback' => 'get_all_shops'
));
});
function get_all_shops(){
$output = array();
$shops = new WP_Query(array(
'post_type' => array('commerces','restaurants'),
'posts_per_page' => -1,
'order' => 'DESC',
));
if (empty($shops->posts)) {
return new WP_Error( 'empty_category', 'There are no posts to display', array('status' => 404) );
}
foreach($shops->posts as $shop){
$id = $shop->ID;
$logoColor = "";
$logoWhite = "";
preg_match('/.+(https[:\/\/]*westland.+)/',get_field("shop-logo-couleur",$id),$logoColor);
preg_match('/.+(https:\/\/westland.+)/',get_field("shop-logo-blanc",$id),$logoWhite);
array_push($output,array(
'basic'=>array(
'id' => $id,
'name' => get_field("shop_name",$id),
'description' => get_field("shop_description",$id),
'logoColor' => $logoColor[1],
'logoWhite' => $logoWhite[1],
'category' => get_the_terms($id,'shops_cats')[0]->name,
'type' => $shop->post_type,
'pageURL' => get_permalink($id),
'giftCard' => get_field("gift_card_participation",$id),
),
));
}
$response = new WP_REST_Response($output);
$response->set_status(200);
return $response;
}
As you can see, the goal is to create a REST endpoint to display store data in JSON format.
But when I’m calling this endpoint, I can see that the value of logoColor is null for every shop.
I just tried to log the value of the get_field() function with error_log(), and it correctly returns the default URL. So I assume the modification is done when the endpoint is called. How can I prevent optimole from changing the URL of this endpoint ?