I currently use the form below :
<form role="search" method="get" class="search-form" action="<?php echo home_url( "https://wordpress.stackexchange.com/" ); ?>">
<input type="search" class="search-field" placeholder="<?php echo esc_attr_x( 'Type Business or Town', 'placeholder' ) ?>" value="<?php echo get_search_query() ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" />
<input type="text" value="<?php echo get_query_var('location'); ?>" name="location" id="location" class="headersearch location" placeholder="<?php esc_attr_e( 'Enter your county..', 'search-cust' ); ?>" />
<input type="hidden" name="post_type" value="business" />
<input type="submit" class="button radius tiny success" style="width: 100%;" value="<?php esc_attr_e( 'Search...', 'search-cust' ); ?>" />
</form>
I also have this in functions.php :
function custom_cpt_search( $query ) {
if ( is_search() && $query->is_main_query() && $query->get( 's' ) ){
$query->set('post_type', array('business'));
}
return $query;
};
add_filter('pre_get_posts', 'custom_cpt_search');
Ideally id like to have the second input as a drop-down taxonomy list instead of a type-in input, Ideally listing ALL categories in the taxonomy instead of only categories with posts in them. With the first dropdown item being a placeholder such as “Select your county”.
Edited to make clearer, and remove part of the question I have resolved.
2 Answers
I think you can do it in two following steps:
-
In HTML code just add a dropdown with the values you need.
<select name="yourselect"><option value="1">Value 1</option> ... </select>
-
In your
pre_get_posts
filter addtax_query
parameter like thisif( !empty( $_GET['yourselect'] ) ) { $query->set('tax_query', array( array( 'taxonomy' => 'YOUR_TAXONOMY_NAME', 'field' => 'id', // or slug if you want 'terms' => $_POST['yourselect'] ) ); }
I’m not sure if it is a good idea to configure this way the default WordPress search. A couple months ago I already implemented similar functionality using AJAX filters. Example is here, hope it helps https://rudrastyh.com/wordpress/ajax-post-filters.html