Its works only for keywords. When i update this to category search means not worked. my page screenshot is given below.
I want to make this type of header search. so please help me anyone
1 Answer 1
I just made it for a client, you’ll have to do it on the pre_get_posts action. That means you will add parameters to the WordPress query before it returns the posts.
<form role="search" method="get" action="<?php echo esc_url( home_url( "https://wordpress.stackexchange.com/" ) ); ?>">
<!-- With this hidden input you can define a specific post type. -->
<input type="hidden" name="post_type" value="your_post_type" />
<input name="s" type="text" />
<select name="category">
<!-- Insert here all option tags you want, with category slug as value -->
</select>
<button type="submit">Submit</button>
</form>
Once submitted, $_GET[] will contain s, post_type and category. s and post_type are used by default WP search, and category will be using in pre_get_posts() to add parameters to WP query.
So finally WP query will consider:
s : your keywords.
post_type : avoids mixing results if several post types have a same taxonomy.
category : the taxonomy you’re searching on.
For WooCommerce
Replace the action attribute of your <form> tag by: