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">
                    <option>5</option>
                    <option>10</option>
                    <option>25</option>
                    <option>50</option>
                    <option>100</option>
            <input type="submit" class="submit" name="submit" id="sul-searchsubmit" value="Search Member" />
            <div id="suggesstion-box"></div>
        </div>
    </form>

PHP WordPress Code

<?php
                    $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"
                                array(
                                    'key'     => 'state',
                                    'value'   => $search,
                                    'compare' => '=',
                                ),
                                array(
                                    'key'     => 'zip',
                                    'value'   => $search,
                                    'compare' => '=',
                                ),
                                array(
                                    '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?


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: