Create Custom Menu in WordPress Dashboard
Complete tutorial on create custom menu in wordpress dashboard. Discover practical examples, implementation tips, and expert advice for WordPress and WooCo
Read More โCreate powerful search form with keyword and custom taxonomy filtering using custom shortcode and pre_get_posts action hook. Build dropdown with get_terms for custom taxonomy options, capture user selections with $_GET parameters, modify main search query using tax_query array for taxonomy filtering, sanitize inputs with sanitize_text_field for security, combine keyword search with taxonomy filtering for precise results, style form with custom CSS for professional appearance, and implement flexible search functionality for custom post types.
Learn how to build a custom WordPress search form with both a keyword input field and a custom taxonomy filter. This guide walks through the process of creating the form, modifying the query, and applying filters using the pre_get_posts action hook.
First, we’ll create a shortcode for the search form that includes both a keyword input and a dropdown for the custom taxonomy filter.
function custom_search_form_shortcode() {
ob_start();
?>
<form role="search" method="get" class="search-form" action="<?php echo esc_url(home_url('/')); ?>">
<div class="search-form-row">
<label for="search-keyword">Search Keyword:</label>
<input type="text" id="search-keyword" name="s" placeholder="Enter your keyword..." value="<?php echo get_search_query(); ?>" />
</div>
<div class="search-form-row">
<label for="search-taxonomy">Filter by Custom Taxonomy:</label>
<?php
$taxonomy = 'your_custom_taxonomy'; // Replace this with your actual taxonomy name
$terms = get_terms($taxonomy, array('hide_empty' => false));
?>
<select name="custom_taxonomy" id="search-taxonomy">
<option value="">All <?php echo $taxonomy; ?> Terms</option>
<?php foreach ($terms as $term) : ?>
<option value="<?php echo esc_attr($term->slug); ?>" <?php selected($term->slug, get_query_var('custom_taxonomy')); ?>>
<?php echo esc_html($term->name); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="search-form-row">
<button type="submit" class="search-submit">Search</button>
</div>
</form>
<?php
return ob_get_clean();
}
add_shortcode('custom_search_form', 'custom_search_form_shortcode');
With this code, you’ve created a simple search form that includes fields for a keyword search and a custom taxonomy filter. The form can now be placed anywhere using the shortcode [custom_search_form].
To filter search results based on the keyword and taxonomy selected, we need to modify the query using the pre_get_posts hook.
function custom_search_pre_get_posts($query) {
if (is_admin() || !$query->is_main_query()) {
return;
}
if ($query->is_search()) {
// Keyword search modification
if (!empty($_GET['s'])) {
$query->set('s', sanitize_text_field($_GET['s']));
}
// Custom taxonomy filter
$taxonomy = 'your_custom_taxonomy'; // Replace with your actual taxonomy name
if (!empty($_GET['custom_taxonomy'])) {
$query->set('tax_query', array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => sanitize_text_field($_GET['custom_taxonomy']),
),
));
}
}
}
add_action('pre_get_posts', 'custom_search_pre_get_posts');
This function checks if the search query is active and then modifies it to include the custom taxonomy filter, allowing results to be filtered accordingly. The keyword filter is applied automatically using the s parameter in the query.
To improve the appearance of the search form, you can add the following CSS styles to your theme’s style.css file:
.search-form {
display: flex;
flex-wrap: wrap;
margin-top: 20px;
}
.search-form-row {
margin-bottom: 12px;
}
.search-form label {
font-weight: bold;
margin-bottom: 5px;
display: block;
}
.search-form input[type="text"],
.search-form select {
padding: 8px;
width: 250px;
}
.search-form .search-submit {
padding: 8px 20px;
background-color: #0073aa;
color: #fff;
border: none;
cursor: pointer;
}
.search-form .search-submit:hover {
background-color: #005a88;
}
This CSS will make the search form more visually appealing by ensuring consistent styling and responsiveness. You can further customize these styles based on your theme’s needs.
Now that you’ve created and customized your search form, you can display it on any post, page, or widget by adding the following shortcode:
[custom_search_form]
This shortcode will render the search form, allowing users to search by keyword and filter results by the custom taxonomy you’ve set up. Be sure to replace 'your_custom_taxonomy' with your actual taxonomy name in both the form and the query modification steps.
To better understand custom taxonomies and their use in WordPress, you may find these resources helpful:
These resources will help you further customize and extend your WordPress search forms, and provide additional examples and best practices for using custom taxonomies and modifying queries. 
Search our archives or reach out to our team for solutions and expert advice.