Support » Developing with WordPress » How to search for listings by multiple values from search bar

  • Hy, I have a search bar at inventory page which is filtering posts based on their title for now.

    The listings also have a value of stock number set.

    So, what I want is that I want the user to be able to search listings either by entering the listing title or listing stock number.

    I’m not able to figure out what should be the meta query for it because the logic will be in inventory search bar only.

      /* Search Text */
                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',
                    array_push($args, $data); 

    this is the code I’m using right now to search by title. Any idea how can I make it to search by stock number as well?


Viewing 1 replies (of 1 total)
  • Moderator bcworkz


    You can modify the search query through the “pre_get_posts” action, which might be what you’re doing now. I cannot tell from an out of context snippet. As long as the search form is sending adequate data, it doesn’t really matter much in what way it was sent. The action callback can transform as necessary.

    I assume you need OR logic for this search? WP mostly uses AND logic. If you add a meta_query arg, it will be ANDed with the search logic. The search term would need to be both in the title and in post meta. To get OR logic, you can stay with adding a normal meta_query arg, then patch up the resulting SQL through the “posts_where” filter. Change the offending AND to OR.

    A normal meta_query arg is documented on the WP_Query doc page. Find the Custom Fields Parameter item in the contents and jump to it. (sorry, deep linking doesn’t work right on that doc page). You’ll find examples there on correct usage.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.