php - Filter posts by multiple checkbox categories - wordpress -


i set widget filter post categories. let's say, have 2 different categories, "countries" , "length of stay" sub categories. here example of have:

enter image description here

what want, filter posts multiple categories. so, if user checking country "laos" , length of stay of "2-4days", retrieve posts category "laos" and category "2-4days" has been attached.

i tried use query multiple taxonomies pluging. however, plugging retrieving posts "laos" category , posts length of stay of "2-4days".

i know, can filter post query, but, need create widget submit button. also, customise remove parent categories , display them title (remove checkbox of "countries" , "length of stay" , add them specific class)?

working query:

<?php // cat 42=laos      cat 57=2-4days     <?php $my_query_1 = new wp_query(array('category__and' => array(42,57))); ?>     <?php while ($my_query_1->have_posts()) : $my_query_1->the_post(); ?>     <a href="<?php the_permalink() ?>" rel="bookmark" title="permanent link     <?php the_title(); ?>"><?php the_title(); ?><?php the_excerpt(); ?></a>     <?php endwhile; ?> 

thanks helping

what have done :

  1. i create new side-bar want add new form
  2. i create template "sidebar-filters.php" of new side bar. here code had inside it.

    <div id="secondary" class="widget-area" role="complementary"> i'm sidebar filters <?php      $current_countries = get_query_var('countries');     $current_stays = get_query_var('stays');      $countries = get_categories('child_of=62');     $stays = get_categories('child_of=63'); ?>  <form action="<?php the_permalink(); ?>" id="filterform" method="post">         <ul>                <li>                     <label><b>countries</b></label>               </li><br/>               <?php                 foreach ($countries $country) { //                  var_dump($country->cat_id); //                  var_dump($country->name);                     echo sprintf('<li><input type="checkbox" name="countries[]" id="checkbox_%s" value="%s" %s',$country->name , $country->cat_id, set_checked($current_countries, $country));                     echo sprintf('/><label for="checkbox_%s">%s</label></li>',$country->name,$country->name );                 }                  ?>                   </ul><br/><br/>         <ul>                <li>                        <label><b>length of stay</b></label>               </li><br/>               <?php                 foreach ($stays $stay) { //                  var_dump($stay->cat_id); //                  var_dump($stay->slug);                     echo sprintf('<li><input type="checkbox" name="stays[]" id="checkbox_%s" value="%s" %s',$stay->slug , $stay->cat_id, set_checked($current_stays, $stay));                     echo sprintf('/><label for="checkbox_%s">%s</label></li>',$stay->slug,$stay->name );                 }                  ?>                                       </ul><br/><br/>             <ul>                <li>                        <button type="submit">send email</button>                </li>         </ul>         <input type="hidden" name="submitted" id="submitted" value="true" /> </form>  

  3. and here code had in function.php

function my_query_vars($vars) { array_push($vars, 'countries', 'stays'); return $vars; } add_filter('query_vars', 'my_query_vars');

function set_checked($arr, $value) { $checked = ''; if (!empty($arr) && in_array($value, $arr)) $checked = 'checked'; return $checked; }

function my_query($query) {   // can use is_archive() or whatever need here   if ($query->is_main_query() && is_search()) {      $cat_and = array();      foreach (array('countries', 'stays') $variable) {       $categories = get_query_var($variable);        if (!empty($categories)) {         $cat_and = array_merge($cat_and, $categories);       }     }      if (!empty($cat_and)) $query->set('category__and', $cat_and);   } } add_action('pre_get_posts', 'my_query');  but, i'm retrieving nothing moment, doing wrong? 

here's basic idea form:

first have register new query vars can use , have filters in url (that can later rewrite if needed):

add_filter('query_vars', 'my_query_vars'); function my_query_vars($vars) {   array_push($vars, 'countries', 'stays');   return $vars; } 

then create form , print categories using array name each group of checkboxes. need make sure restore checkboxes checked after submitting form, can on same page.

function set_checked($arr, $value) {   $checked = '';   if (!empty($arr) && in_array($value, $arr)) $checked = 'checked';   return $checked; }  $current_countries = get_query_var('countries'); $current_stays = get_query_var('stays');  $countries = get_categories('child_of=id_for_countries'); $stays = get_categories('child_of=id_for_length-of-stay');  // begin form  foreach ($countries $country) {   echo sprintf('<input type="checkbox" name="countries[]" value="%s" %s', $country->cat_id, set_checked($current_countries, $country)); }  foreach ($stays $stay) {   echo sprintf('<input type="checkbox" name="stays[]" value="%s" %s/>', $stay->cat_id, set_checked($current_stays, $stay)); }  // end form 

finally need modify query filter according variables:

add_action('pre_get_posts', 'my_query'); function my_query($query) {   // can use is_archive() or whatever need here   if ($query->is_main_query() && is_search()) {      $cat_and = array();      foreach (array('countries', 'stays') $variable) {       $categories = get_query_var($variable);        if (!empty($categories)) {         $cat_and = array_merge($cat_and, $categories);       }     }      if (!empty($cat_and)) $query->set('category__and', $cat_and);   } } 

i haven't tested should work, hope helps.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -