Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 :

<?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 !!!

share|improve this question
add comment

1 Answer

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');
share|improve this answer
    
thank you very much, sample code .. really helped me :) –  Reddy 'Rk' Apr 11 '11 at 15:52
    
Glad i could help –  Bainternet Apr 11 '11 at 16:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.