WordPress: Add Category & Keyword Filters to Your Search Form

Search Form With Keyword And Category Fields In WordPress

In WordPress, you can create a custom search form that allows users to filter content by both keywords and categories. This guide will walk you through the process of creating a shortcode for a search form with these fields.

Step 1: Create a Custom Function for the Search Form

To begin, you need to define a custom function that generates the HTML for the search form. This form will include a keyword input field and a category dropdown.

 

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">Keyword:</label>
            <input type="text" id="search-keyword" name="s" placeholder="Enter keyword..." value="<?php echo get_search_query(); ?>" />
        </div>
        <div class="search-form-row">
            <label for="search-category">Category:</label>
            <?php
            $args = array(
                'show_option_all' => 'All Categories',
                'taxonomy' => 'category',
                'name' => 'cat',
                'id' => 'search-category',
                'orderby' => 'name',
                'class' => 'postform',
                'hierarchical' => true,
                'depth' => 2,
                'hide_empty' => false,
            );
            wp_dropdown_categories($args);
            ?>
        </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');

 

This function creates a form with a keyword input field and a category dropdown. The form submits the search query to the WordPress search results page, where the results are filtered by both the keyword and selected category.

 

Step 2: Add CSS Styles (Optional)

You can customize the appearance of the search form by adding some CSS styles. Below is an example of how to style the form to make it look more appealing. Add these styles to your theme’s style.css or a custom CSS file.

.search-form {
    display: flex;
    flex-wrap: wrap;
    margin-bottom: 20px;
}

.search-form-row {
    margin-bottom: 10px;
}

.search-form label {
    font-weight: bold;
    display: block;
    margin-bottom: 5px;
}

.search-form input[type="text"],
.search-form select {
    padding: 8px;
    width: 100%;
    max-width: 300px;
}

.search-form .search-submit {
    padding: 8px 20px;
    background-color: #0073aa;
    color: white;
    border: none;
    cursor: pointer;
    transition: background-color 0.3s;
}

.search-form .search-submit:hover {
    background-color: #005a88;
}

 

This CSS styling will make the form responsive and easy to use. You can adjust the width of the inputs and buttons as per your site’s design needs.

Step 3: Use the Shortcode

Once you have added the above code, you can use the [custom_search_form] shortcode anywhere on your WordPress site—whether in a post, page, or widget. Just paste the shortcode into the content editor where you want the form to appear.

[custom_search_form]

This will render the search form on your site, allowing users to enter a keyword and select a category to filter their search results.

 

Conclusion

With these steps, you can easily create a custom search form in WordPress that lets users search by both keyword and category. Feel free to modify the code and styling to better match the design of your site.

 

Related External Links

Related Blogs

Elementor shortcodes

Elementor is a popular page builder plugin for WordPress that allows you to create and customize web pages using a drag-and-drop interface. Elementor provides a wide range of built-in widgets and elements, and you can also create your own custom elements using shortcodes.

Ultimate Guide to Custom Meta Boxes in WordPress

In WordPress, `add_meta_boxes` is an action hook that allows developers to add custom meta boxes to different post types’ edit screens. Meta boxes are additional sections on the post editor screen where you can add and display custom fields or other types of data related to the post.

Jquery Form Validator With User Name Exist User Email Exist

To implement jQuery Form Validator with the specified validation rules, including required, minlength, email, equalTo, custom validation, confirm password, username pattern, email existence in the database in WordPress, and username with no numeric value, you’ll need to combine client-side validation using jQuery Form Validator with server-side validation using PHP and WordPress functions.

Request A Quote

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.