Search code examples
phpwordpresssearchlimitsearch-form

WordPress Limit Search to Category of Current Post


I have a WP theme which uses its own searchform. As you can see in the code below, it is already set to search within the category when the search form is displayed in this category.

I have also set the search form to be displayed above single posts, but when I search in this form, I get results from all categories. I want the search form to search only within the category of that post.

I have searched everywhere but cannot find a solution. Thank you in advance!

<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<?php // Check to see if category, if yes, then modify the search parameters ?>
<?php if ( is_category() ) : ?>
<input type="hidden" name="cat" value="<?php echo esc_attr( get_query_var( 'cat' ) ); ?>" />
<?php $stext = esc_attr( sprintf( __( 'Search Knowledgebase for %s&hellip;', 'ipt_kb' ), single_cat_title( '', false ) ) ); ?>
<?php endif; ?>
<div class="form-group">
    <div class="input-group input-group-lg">
        <input type="search" class="search-field form-control" placeholder="<?php echo $stext; ?>" value="<?php echo esc_attr( get_search_query() ); ?>" name="s" />
        <span class="input-group-btn"><button type="submit" class="btn btn-default"><span class="ipt-icon-search"></span></button></span>
    </div>
</div>


Solution

  • Edit search.php and add below code.. loop over the result.

    below code will search keyword with in defined taxonomies and its terms and the post type post

    function getAllTermsByTaxonomy($taxonomy) {
        $taxonomy_terms = get_terms($taxonomy, array(
            'hide_empty' => 0,
            'fields'     => 'ids'
        ));
        return $taxonomy_terms;
    }
    // Get all term ID's in a given taxonomy
    $terms_of_diadora = getAllTermsByTaxonomy('diadora');
    $terms_of_iadera  = getAllTermsByTaxonomy('iadera');
    $terms_of_borik   = getAllTermsByTaxonomy('borik');
    $terms_of_adriana = getAllTermsByTaxonomy('adriana');
    // Use the new `tax_query` WP_Query argument
    $args  = array(
        's'         => get_query_var('s'),
        'post_type' => 'post',
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'diadora',
                'field'    => 'term_id',
                'terms'    => $terms_of_diadora
            ),
            array(
                'taxonomy' => 'iadera',
                'field'    => 'term_id',
                'terms'    => $terms_of_iadera,
            ),
            array(
                'taxonomy' => 'borik',
                'field'    => 'term_id',
                'terms'    => $terms_of_borik,
            ),
            array(
                'taxonomy' => 'adriana',
                'field'    => 'term_id',
                'terms'    => $terms_of_adriana,
            ),
        ),
    );
    $query = new WP_Query($args);
    // Loop through the post