1

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座地下
2

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.

  • Thanks! Tried and it works. However, when the value is "15", it returns some addresses that does not include 15. – Raptor Jun 17 '15 at 6:02
  • I can't answer that without seeing the data. The LIKE operator may be too broad. You should use = if possible. – s_ha_dum Jun 17 '15 at 6:12
  • Updated sample result in question (sorry, it's in Chinese). – Raptor Jun 17 '15 at 6:16
  • You've probably got an encoding/collation issue with the database. That is not something I have a lot of experience with. – s_ha_dum Jun 17 '15 at 6:19
  • 1
    Ok. Now I can answer. What you have are serialized strings. MySQL can't parse those. You can't really search data that is stored that way. In short, it is a ridiculous way to store data. – s_ha_dum Jun 17 '15 at 6:21
1

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).

  • Oh, I don't even know it's able to use RegEx. Nice answer, thanks. – Raptor Jun 18 '15 at 2:07
0

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

  • 1
    This is virtually identical to my answer. What additional information do you think it provides? – s_ha_dum Jun 17 '15 at 5:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.