0

I’m having have a checkbox filtering issue in wordpress - Please have a look at the details below - Any help would greatly be appreciated!

What I’ve built:

I created three custom taxonomies of topics, skateboard and wheels.

I have a page that displays the taxonomies with the slug that looks something like this:

/topics/skateboard/wheels

I created another taxonomy called regions with three locations North America , South America , All of America.

I rendered the regions as check boxes onto the page for filtering.

My issue:

The page /topics/skateboard/wheels is rendering all of the posts associated to its respective taxonomies yet when I select North America - The query returns all of the posts from the database that has North America checked.

I can’t get the filter to only return posts associated to this slug topics/skateboard/wheels.

So i’d like to return posts with North America checked marked only associated to topics -> skateboards -> wheels.

So all of the wheels in North America.

Form with checkboxes:

          <?php 
            
            if($_GET['regions[]'] && !empty($_GET['regions[]'])) {
              $regions = $_GET['regions[]'];
            }
            
            ?>

            <form action = "" method="GET">
              <?php 
                $terms = get_terms([
                  'taxonomy' => 'regions',
                  'hide_empty' => false
                ]);
                foreach ($terms as $term) :
              ?>
              <label>
                  <input
                  type="checkbox"
                  name="regions[]"
                  value="<?php echo $term->slug?>"
                  
                  <?php 
                  checked(
                    (isset($_GET['regions']) && in_array($term->slug, $_GET['regions']))
                    ) ?>
                  /> 
                  <?php echo $term->name; ?>
            </label>
            <?php endforeach; ?>
            <button class="uk-button-filter" type="submit"> Apply Filter </button>
            </form>

The Query:

  <?php
              $terms = get_terms( 'regions' ); 
              $term_ids = wp_list_pluck( $terms, 'slug' );
              $args_one = 
              array(
                  'post_type' => 'post',
                  'tax_query' => array(
                      array(
                          'taxonomy' => 'regions',
                          'field' => 'slug',
                          'terms' => $_GET ? $_GET['regions'] : $term_ids,
                      ),
                      'relation' => 'AND',
                  ),
              );

          ?>

The Results:

<?php 
      $the_query = new WP_Query( $args_one );

    // The Loop
    if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post(); ?>

      <li>

        <?php echo get_the_title();  ?>

      </li>


        
    <? }
    } else {
    // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
                            
    ?>  

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.