1

I feel I'm close in solving this issue but kinda hit a wall.

I have a loop on the examlpe.com/blog site and I'd like to be able to filter it using the category checkboxes.

Here is my loop

<h3 class="header__heading | u-margin__bottom--400">Latest posts</h3>
  <div class="grid__col--span-2">
    <?php
        $latestPostLoop = new WP_Query(array('posts_per_page' => 6));
        while ($latestPostLoop->have_posts()) : $latestPostLoop->the_post(); ?>
    <?php   $ids[] = get_the_ID(); ?>
    <?php include(locate_template('partials/summary--article.php', false, false)); ?>
    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>
  </div>

The form:

<?php
// Get all "Topic" terms
$category_terms = get_terms(
  array(
    'taxonomy'   => 'category',
    'hide_empty' => false,
  )
);



// Get active taxonomy filters
$category_selected = get_query_var('category');
if ( ! is_array($category_selected)) {
  $category_selected = array($category_selected);
}

$archive = get_post_type_archive_link('post');

?>
<form action="<?php echo esc_url($archive); ?>" method="get">

   <div>
    <input type="hidden" name="post_type" value="post">
    <?php
    $search = trim(get_search_query());
    if ( ! empty($search)) {
    ?>
    <input type="hidden" name="s" value="<?php echo esc_attr($search); ?>">
    <?php } ?>

  <fieldset class="form__fieldset">
    <legend class="form__legend">Category</legend>
     
    <div class="form__item--checkbox">
    <?php 
      $selected = empty($category_selected) || empty($category_selected[0]) ? 
       'checked="checked"' : '';?>
       <input type="checkbox" value="" id="checkbox-category-1" class="form__checkbox" 
        name="category" <?php echo $selected ?>>
       <label for="checkbox-category-1" class="form__label">All</label>
    </div>

      <?php
      $count = 2;
      foreach ($category_terms as $term) {
        $selected = in_array($term->slug, $category_selected) ? 'checked="checked"' : '';?>

      <div class="form__item--checkbox">
        <input type="checkbox" value="<?php echo esc_attr($term->slug); ?>" id="checkbox-topic- 
       <?php echo $count; ?>" name="category[]" class="vf-form__checkbox" <?php echo $selected ?>>             
        <label for="checkbox-topic-<?php echo $count; ?>" class="vf-form__label"><?php echo 
      esc_html($term->name); ?>
      </label>
    </div>

      <?php 
        $count++;
      } ?>
      </fieldset>

    <button class="button" type="submit">
      <?php esc_html_e('Apply filter', 'theme'); ?>
    </button>

  </div>
</form>

After submitting the form the URL I see is .../blog/?post_type=post&category=category name

However, this doesn't have any effect and I can see all the posts.

Any help much appreciated!

2
  • Where do you use the $_GET values? And what is $category_selected?
    – moped
    Commented Jan 25, 2021 at 9:24
  • Sorry, forgot to include it. The form is updated now. Commented Jan 25, 2021 at 10:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.