How to search city based on Geographic location?

I am new in PHP and i would like to search cities that are located within a certain number of miles?

For. e.g.

When people sign up, they will register will their physical address. When someone is searching for a member they will search by city and Search functionality will be based on distance away.

My Html Code

<form method="get" id="sul-searchform" action="<?php echo home_url( '/search-member/' ); ?>">
        <div class="frmSearch">
            <input type="text" id="search-box" name="as"  placeholder="City, state or zip" />
            <label>Select a distance in miles:</label>
                    <select name="distance">
            <input type="submit" class="submit" name="submit" id="sul-searchsubmit" value="Search Member" />
            <div id="suggesstion-box"></div>

PHP WordPress Code

                    $search = ( isset($_REQUEST["as"]) ) ? sanitize_text_field($_REQUEST["as"]) : false ;

                  if($search ){

                        $args = array(
                            'posts_per_page'   => -1,
                            'orderby'          => 'post_date',
                            'order'            => 'DESC',
                            'post_status'      => 'publish',
                            'post_type' =>'custom_post',
                            'meta_query' => array(
                                'relation' => 'OR', // Optional, defaults to "AND"
                                    'key'     => 'state',
                                    'value'   => $search,
                                    'compare' => '=',
                                    'key'     => 'zip',
                                    'value'   => $search,
                                    'compare' => '=',
                                    'key'     => 'city',
                                    'value'   => $search,
                                     'compare' => '=',

                $enterprise_posts = get_posts( $args );

i do not have any idea how to do this , does anyone know about it then please share me code with steps by steps?

Read more here: How to search city based on Geographic location?

Leave a Reply

Your email address will not be published. Required fields are marked *