I wanna explain about my search box, actually what i want to make.
Ive a search input and few check boxes to filter the search result.
i need to search like this :
[Country1] AND ( [venue1] OR [venue2] ) AND ( [category1] OR [category2] ) AND [topic1] AND ( BETWEEN $datefrom & $datetill )
Markup :
<form role="search" method="GET" id="searchform" action="<?php echo home_url( '/' ); ?>" >
<input class="form-control" type="text" name="s" id="s" <?php if(is_search()) { ?>value="<?php the_search_query(); ?>" <?php } else { ?>value="Enter keywords …" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"<?php } ?> /><br />
<button class="btn btn-primary" type="submit" id="searchsubmit" value="Search" > Search </button>
<h4>Countries</h4>
<input name="location" value="Country1" type="radio" >Country1</label>
<input name="location" value="Country2" type="radio" >Country2</label>
<input name="location" value="Country3" type="radio" >Country3</label>
<input name="location" value="Country4" type="radio" >Country4</label>
<h4>Venues</h4>
<input name="location" value="venue1" type="checkbox" >venue1</label>
<input name="location" value="venue2" type="checkbox" >venue2</label>
<input name="location" value="venue3" type="checkbox" >venue3</label>
<input name="location" value="venue4" type="checkbox" >venue4</label>
<h4>Custom taxonomy/Category</h4>
<?php
$categories = get_terms( 'eventcategory', array(
'orderby' => 'title',
'hide_empty' => 0,
) );
foreach ($categories as $va) {
$cat_code = "<label>";
$cat_code .= "<input name='category_name' type='checkbox' value='";
$cat_code .= $va->name;
$cat_code .= "' /> ";
$cat_code .= $va->name;
$cat_code .= "</label> ";
echo $cat_code;
}
?>
<h4>Topic</h4>
<input name="location" value="Topic1" type="radio" >Topic1</label>
<input name="location" value="Topic2" type="radio" >Topic2</label>
<input name="location" value="Topic3" type="radio" >Topic3</label>
<input name="location" value="Topic4" type="radio" >Topic4</label>
<h4>Date</h4>
<input name="datefrom" id="datepicker1" type="text" class="form-control" />
<input name="datetill" id="datepicker2" type="text" class="form-control" />
</form>
functions.php
function search_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ( $query->is_search ) {
$searchfield = $_GET['s'];
$country = $_GET['location'];
$venue_s = $_GET['venue'];
$industry = $_GET['industry'];
$datefrom = $_GET['datefrom'];
$datetill = $_GET['datetill'];
$filter_cat = $_POST['category_name'];
$rel = 'OR';
$metaQuery = array( 'relation' => $rel,
array( 'key' => 'location', 'value' => $country, 'compare' => 'IN' ),
array( 'key' => 'venue', 'value' => $venue_s, 'compare' => '==' ),
array( 'key' => 'industry', 'value' => $industry, 'compare' => '==' ),
array( 'key' => 'datefrom', 'value' => array($datefrom, $datetill), 'compare' => 'BETWEEN' )
);
$query->set( 'meta_query' , $metaQuery );
}
}
}
add_action('pre_get_posts','search_filter');
My Problem is it so weird, i don't understand
Aucun commentaire:
Enregistrer un commentaire