Hi, I have a post type that includes a google maps field. I have got my maps displaying fine but I’d like to add a search so that users can search my custom posts by location. Is this possible?
Hi @twizzlebird
You can always query the posts based on custom fields. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/query-posts-custom-fields/.
You can use the $_GET variable for the search keyword and use it like this:
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'cpt',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'google_map',
'value' => $_GET['location'],
'compare' => 'LIKE',
),
),
));
The URL should be something like this: http://example.com/path-where-you-put-the-code/?location=locationkeyword
I hope this helps 🙂