• I want to create search form (in WordPress) with Google Place Api, where you can type city or state or zip-code. In the search results page I have had more advanced form with 2 fields (city or state or zip-code input, select with taxonomy “services”)

    So I create Custom Post Type “Shop” with taxonomies (“state” and “services”).

    For example – CPT post: “Shop Name” – State: “Illinois” – Services: “Service 1”, “Service 2”

    I’ve created searchform.php file (simple form with one field): pastebin.com/cUPd2SWS

    File search.php contains second, more advanced form (two fields): pastebin.com/N7u18b2y

    Javascript function to set value in hidden field

    var state = document.getElementById('state');
    var stateData = document.getElementById('s');
    var options = {
        componentRestrictions: {
            country: 'us'
        }
    };
    
    var autocomplete = new google.maps.places.Autocomplete(state, options);
    
    autocomplete.addListener('place_changed', function() {
        var place = autocomplete.getPlace();
    
        if (place.geometry) {
            stateData.value = place.place_id + ':' + place.geometry.location.lat() + ':' + place.geometry.location.lng();
        } else {
            stateData.value = '';
        }
    });

    Now in functions.php file I created sql query: https://pastebin.com/iNtPdn95

    When I type in search for example only state by text (without autocomplete) I have correct results.

    But when I type for example city “Chicago” and select autocomplete “Chicago, Illinois, United State” my results is wrong

    I var_dump WP_Query object on search results page

    test.dev/?s=ChIJ7cv00DwsDogRAMDACa2m4K8%3A41.8781136%3A-87.62979819999998&state=Chicago%2C+Illinois%2C+United+State

    WP_Query object https://pastebin.com/9Lzf8Pbd

    I think that problem is in public ‘tax_query’ array. How Can I resolve this that state taxonomy select only state.

    And how can I select state when user send zip-code (without autocomplete)?

  • The topic ‘Search Google Place API’ is closed to new replies.