Support » Fixing WordPress » Combine 2 Search forms.

  • Resolved bananenkopf

    (@bananenkopf)


    Hey,

    my project is a fusion of 2 searchforms into 1.

    I use the simple locator (SL) (search by location) plugin. Unfortunately the support is very bad, so i tried to solve this problem “theoretically”, how this could be done in general.

    So wordpress outputs a form for keyword search, SL outputs its form for location search, they both generate /?x=… from home URL, i only have to find a way to get the ?s= into this query.

    To summarize:
    SL: Enter a city and i will output you every post thats related to that geolocation or next to it.
    WP: Enter a keywprd and i will output every post thats related to the keyword

    And I want to combine it like: Enter a Keyword here and a City there and i will output every post thats related to the keyword and related to the city/next to it.

    I know thats not one of the easy to answer questions, but maybe there will be some good inputs that will bring me on the right hook?

Viewing 6 replies - 1 through 6 (of 6 total)
  • You can modify the search query at searching time. Please try with this hook function and query :

    add_action('pre_get_posts', 'modified_search');
    function modified_search($query){
        global $wp_query;
        if($query->is_search){
            global $wpdb;
            $original_query = get_search_query();
            $modified_query = preg_replace("/(s|S)/", "$", $original_query);
            $new_query = "
                SELECT $wpdb->posts.ID
                FROM $wpdb->posts
                WHERE $wpdb->posts.post_status = 'publish'
                AND (($wpdb->posts.post_title LIKE '%$original_query%') OR ($wpdb->posts.post_content LIKE '%$original_query%') OR ($wpdb->posts.post_title LIKE '%$modified_query%') OR ($wpdb->posts.post_content LIKE '%$modified_query%'))
                ORDER BY $wpdb->posts.post_date DESC
                LIMIT 0, 10
            ";
            $results = $wpdb->get_results($new_query);
            $post_ids = array();
            foreach ($results as $post_id){
                $post_ids[] = $post_id->ID;
            }
            $query->set('post__in', $post_ids);
        }
    }
    • This reply was modified 1 year, 4 months ago by Steve Stern.

    Hey cleancoded,

    thanks for the snippet.

    But where/how to call the ‘modified’ function?

    Add this snippet in functions.php file and change the database query according to your requirements.

    Note: Please take functions.php backup before working on it because a simple mistake can affect the whole site.

    Hey,

    I’m familiar with functions.php, but not that much with wp_query.

    So i’ve got a mental blank, could you just give me a hint on this what variable to change

    change the database query

    thanks

    You need to modify this Query :

          $new_query = "
                SELECT $wpdb->posts.ID
                FROM $wpdb->posts
                WHERE $wpdb->posts.post_status = 'publish'
                AND (($wpdb->posts.post_title LIKE '%$original_query%') OR ($wpdb->posts.post_content LIKE '%$original_query%') OR ($wpdb->posts.post_title LIKE '%$modified_query%') OR ($wpdb->posts.post_content LIKE '%$modified_query%'))
                ORDER BY $wpdb->posts.post_date DESC
                LIMIT 0, 10
            ";

    As you mentioned in your comments : Enter a Keyword here and a City there and it will output every post that’s related to the keyword and related to the city/next to it.
    So, you have to edit this query according to your requirements. Check the field name and table in database and modify query according to expected result. Check this given URL, it will helps you to understand the WP Custom Queries : https://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

    Didn’t need to modify the query, just put the default search input

    <input type="text" placeholder="Search for..." name="s" id="s" />

    inside the other searchform. Done.

    the essential part is name="s"

    WP will do the rest.

    • This reply was modified 1 year, 3 months ago by bananenkopf.
    • This reply was modified 1 year, 3 months ago by bananenkopf. Reason: code correction
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Combine 2 Search forms.’ is closed to new replies.