• Hy there, I hope you are doing well

    I’ve a search bar on inventory page of my website.

    Right now the search bar is searching for vehicles based on their title. I want the same search bar to search for vehicles based on their ‘stock-number and ‘vin-number’ also.

    like if someone searches for ‘Ford’ it should show the ford results but if someone searches the vehicle by its stock number like ‘d7923’ then it should also show listing with stock number of ‘d7923’ and same case for the vin number.

    Right now I have this code which is checking the parameter in url and returning the value.

     if(isset($_REQUEST['inventory_search']) && !empty($_REQUEST['inventory_search'])){
                    
                    $inventory_search = $_REQUEST['inventory_search'][0];
                    $args['s'] = $inventory_search;
    
                    $data =  array(
                    	'post_title' => $inventory_search,
                        's' => $inventory_search,
                        'compare' => 'LIKE',
                    );
                        $datastock = array(
                         'key' => 'stock-number',
                         'value' => $inventory_search,
                         'compare' => 'IN'
                     );
    
                    array_push($args, $data); 
                    array_push($args['meta_query'], $datastock); 
                }

    If i remove the first code

    $args['s'] = $inventory_search;

    then its working and showing the listings by their stock number but doing this will not work for search by title.

    Any idea how i can implement this?

    Remember it will be only 1 search bar.

    Thanks

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    “compare” at the same level as “s” isn’t valid and is ignored. “s” queries use LIKE by default anyway. The various parts (title, content, etc.) of a “s” query use OR logic. So far so good. The problem is added query args like “meta_query” are added in with AND logic. The same search term in both “s” and “meta_query” are very unlikely to match anything this way.

    The offending AND needs to be changed to OR for the query to work as you wish. There’s no way to do this with WP_Query args. You need to directly alter the SQL through the “posts_request” or “posts_where” filters.

Viewing 1 replies (of 1 total)
  • The topic ‘How to search for multiple listings by multiple values from single search bar’ is closed to new replies.