Hi,
I'm struggling with the following problem for a while. It's almost working, but still not yet. First of all, sorry for my bad English.
I made this sidebar search form (see code below) for my category page to filter the search results based on the tags which are available within the category.
The first part is working great when I use radio buttons, but I want the form to remember mulitple checkbox fields. Someone suggested name="tag[]"
but that's not working unfortunately. When I use name="tag"
the url output is /?tag=test&tag=test2
instead of /?tag=test,test2
so it won't work properly.
The next problem. I use this function to make a url parameter based on a custom field, but I can't figure out how to use it with multiple keys.
function wpa_filter_home_query( $query ){
if( $query->is_main_query()
&& isset( $_GET['provincie'] ) ) {
$meta_query = array(
array(
'key' => 'provincie',
'value' => array( $_GET['provincie'])
)
);
$query->set( 'meta_query', $meta_query );
}
}
add_action( 'pre_get_posts', 'wpa_filter_home_query' );
And the last problem is that I want to show only keys from custom fields under <h3>Land</h3>
and <h3>Regio</h3>
just like the code only shows tags which are used. I also can't figure out how to let this work for the custom fields.
The form:
<form name="tags" onChange="document.forms.tags.submit();">
<?php
if (is_category()){
$cat = get_query_var('cat');
$yourcat = get_category ($cat);
}
$tag_IDs = array();
query_posts('category_name='.$yourcat->slug);
if (have_posts()) : while (have_posts()) : the_post();
$posttags = get_the_tags();
if ($posttags):
foreach($posttags as $tag) {
if (!in_array($tag->term_id , $tag_IDs)):
$tag_IDs[] = $tag->term_id;
$tag_names[$tag->term_id] = $tag->name;
$tag_slug[$tag->term_id] = $tag->slug;
endif;
}
endif;
endwhile; endif;
wp_reset_query();
if (!empty($tag_IDs)){
echo '<h3>Het meest geschikt voor</h3>';
echo "<input type=\"checkbox\" name=\"tag\" value=\"\"> Show all<br>";
}
foreach($tag_IDs as $tag_ID){
$checked = $tag_slug[$tag_ID];
echo '<input type="checkbox" name="tag" value="'.$checked.'"' ;
if((isset($_GET["tag"])) && $_GET["tag"] == $checked) {
echo ' checked="checked"';
}
echo '> '.$tag_names[$tag_ID].'<br>';
}
?>
<div class="margin25px"></div>
<h3>Land</h3>
<input type="radio" name="land" value=""> Alles weergeven<br>
<input type="radio" name="land" value="nederland" <?php if((isset($_GET["land"])) && $_GET["land"] == "nederland") { echo "checked";}?>> Nederland<br>
<input type="radio" name="land" value="belgie" <?php if((isset($_GET["land"])) && $_GET["land"] == "belgie") { echo "checked";}?>> Belgie
<div class="margin25px"></div>
<h3>Regio</h3>
<input type="radio" name="provincie" value=""> Alles weergeven<br>
<input type="radio" name="provincie" value="gelderland" <?php if((isset($_GET["provincie"])) && $_GET["provincie"] == "gelderland") { echo "checked";}?>> Gelderland<br>
<input type="radio" name="provincie" value="overijssel" <?php if((isset($_GET["provincie"])) && $_GET["provincie"] == "overijssel") { echo "checked";}?>> Overijssel
</form>
Hopefully someone can give me some hints to make this work. It's a great category search form if it's working properly. Sorry if I ask to many questions, but it's all related to each other since it's all meant to make the sidebar filter function work.