How to search map address in custom post type?

I built a custom post type “shop” with Custom Post Type UI plugin and added a field “address” (Google Map type) with Advanced Custom Fields plugin.

From the response of WP_Query('post_type=shop');, I got the following:

$shop = WP_Query('post_type=shop');
while($shop->have_post()) {
  $shop->the_post();
  $location = get_field('map-location');
  if($location != null) {
    echo $location['address'];
  }
}

Here I got a list of addresses. How do I search with this “address”? Let’s say user wants to search shop that is in “London”, how can I achieve this?

Thanks.


UPDATE

Sample result of searching “15”:

  • 筲箕灣東大街15號B地下
  • 九龍觀塘偉業街103號振邦工業大廈地鋪
  • 九龍土瓜灣旭日街一號 瑞英工業大廈第一期A座地下

Solutions Collecting From Web of "How to search map address in custom post type?"

You need a meta_query something like this:

$args = array(
    'post_type' => 'shop',
    'meta_query' => array(
        array(
            'key' => 'map-location',
            'value' => 'London',
            'compare' => 'LIKE' // the = operator might work depending on how your data is stored
        ),
    )
);

Note: I am making some assumptions about how ACF stores data.

You could use regular expression RLIKE:

$val = '15';
$args = array(
    'post_type' => 'shop',
    'meta_query' => array(
        array(
            'key' => 'map-location',
            'value' => 's:7:"address";s:[0-9]+:"[^"]*' . preg_quote( $val ) . '[^"]*";',
            'compare' => 'RLIKE'
        ),
    )
);

although it can fail if your address data contains a double quote. If you always have a lat and a lng field following the address then you could extend the expression to match these and do away with the need for the [^"] bits (replacing them with dots).

You can use meta query to search for a particular field text.

 $val = 'London';
 $args = array(
   'post_type' => 'shop',
   'meta_query' => array(
     array(
        'key' => 'map-location',
        'value' => $val,
        'compare' => 'LIKE' 
     ),
   )
 );
 $query = new WP_Query($arg);

Class Reference/WP Query