Reputation: 299
I am creating a news website at index page i listed all news by categories i created 1 more category for one of my news section that is recipes now what i want is i am creating a search form for that section only that filter the search for only recipe category,how can i do that i wanted to know basically i created a normal search.php page as per wordpress standard code that filter news from through out the theme. please help me out.
here is a code in form page:
<div class="search_box">
<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<div class="input-group">
<input type="text" class="form-control search-bar eng my_control" name="s" id="search" placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder', 'dastak' ); ?>" value="<?php echo esc_attr( get_search_query() ); ?>" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"/>
<span class="input-group-btn">
<input type="submit" class="search-submit btn my-btn" value="<?php echo esc_attr_x( 'Search', 'submit button', 'dastak' ); ?>">
<!-- <button class="btn my-btn" id="searchsubmit" type="button"><span class="glyphicon glyphicon-search"></span></button> -->
</span>
</div><!--input group-->
</form>
</div><!--search box-->
<div class="clearfix"></div>
Upvotes: 2
Views: 6729
Reputation: 4566
You could implement a function to catch the search but defining the category in the category__in array.
global $wp_query;
if(is_search()) :
$cat = array(
'category__in' => array(5) // Where 5 is the ID of your Music category
);
$args = array_merge( $wp_query->query, $cat );
endif;
query_post($args);
OR
You could do it this way through the hidden field - value 22 is the category
<form method="get" id="search form" action="/">
<div>
<input type="text" value="" name="s" id="s" />
<input type="hidden" value="22" name="cat" id="scat" />
<input type="submit" id="search_submit" name="Search" value="Search"/>
</div>
</form>
Upvotes: 6
Reputation: 1070
You have to hook the WordPress WP_Query global instance and add whatever conditions you want.
Start reading about preg_get_posts WordPress hook. You'll find two examples about categories.
Upvotes: 0