Search form with Category and Sub Category

How to create WP Search Form with Category and Sub category
for illustration a search form (for search title Book=title post)
With condition By Keyword, By All Category and Sub Category selected
Where, Sub Category (CD, PDF, EPub,…)
I Look Around for a Week but unsuccessful,
Help me , Please …
=> For My Client blog for CMS Ebook

I Have modified my form like This :

Read More
<?php $media = array(
 'hierarchical'       => 1,
 'parent'             => get_cat_id('Media'),
 'show_option_none'   => ('All Media'),
 'hide_empty'   => 0  ); 
?>

<form method="get" id="searchform" action="<?php bloginfo('url'); ?>/">
  <div>
    <input type="text" value="<?php the_search_query(); ?>" name="s" id="s" />
    <?php wp_dropdown_categories('show_option_none=All Category'); ?>
    <?php wp_dropdown_categories($media); ?>
    <input type="submit" id="searchsubmit" value="search" />
  </div>
</form>

How to Create Function or modified form again ? Without Plugin…
For a detailed answer I am very grateful !!!

Related posts

Leave a Reply

1 comment

  1. First you have to give your dropdown names so:

    <?php $media = array(
     'name'               => 'subcat',
     'hierarchical'       => 1,
     'parent'             => get_cat_id('Media'),
     'show_option_none'   => ('All Media'),
     'hide_empty'   => 0  ); 
    ?>
    
    <form method="get" id="searchform" action="<?php bloginfo('url'); ?>/">
      <div>
        <input type="text" value="<?php the_search_query(); ?>" name="s" id="s" />
        <?php wp_dropdown_categories('name=maincat&show_option_none=All Category'); ?>
        <?php wp_dropdown_categories($media); ?>
        <input type="hidden" id="my_search" name="my_search" value="c_search" />
        <input type="submit" id="searchsubmit" value="search" />
      </div>
    </form>
    

    Then create you search filter

    // Define search filter
    function search_filter( $query ) {
        // only modify your custom search query.
        if ( $query->is_search &&  $_post['my_search'] == "c_search") {
            $args = array(
                    'relation' => 'AND',
                array(
                    'taxonomy' => 'category',
                    'field' => 'id',
                    'terms' => array( $_post['maincat']),
                    'operator' => 'IN'
                ),
                array(
                    'taxonomy' => 'category',
                    'field' => 'id',
                    'terms' => array( $_post['subcat']),
                    'operator' => 'IN'
                )
            );
            $query->set( 'tax_query', $args);
        }
        return $query;
    }
    
    // The hook needed to search_filter
    add_filter( 'the_search_query','search_filter');