Navigation
This is archived content. Visit our new forum.
  • Author
    Posts
  • #127973

    giftofjehovah
    Participant

    Hi,

    How do i add Filter posts by multiple checkbox categories?
    As shown on:
    http://stackoverflow.com/questions/16665868/filter-posts-by-multiple-checkbox-categories-wordpress

    Are there any plugin available?

    Thank you so much!

    Best rgds,
    Vivien

    #127974

    giftofjehovah
    Participant

    I forgot to mention my website!

    http://www.langelir.com

    Thanks again!

    #128078

    Rad
    Moderator

    Hi there,

    Thanks for posting in.

    That is possible by custom development.

    Example, by adding this at your child theme’s functions.php

    function x_filter_by_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() && isset($_GET['cat_select']) ) {
    $query->set( 'cat', implode( ',', $_GET['cat_select'] ) );
    }
    }
    add_action('pre_get_posts', 'x_filter_by_category');

    Then somewhere on your template/or widget add this code. (let say you have wp-sidebar.php at your child theme’s /framework/views/{STACK}/ folder.

    <?php if ( x_get_content_layout() != 'full-width' ) : ?>
    
    <!----- START : THIS IS FILTER FORM UNDER wp-sidebar.php -------->
    <form action="<?php echo home_url()?>">
    <?php 
      $categories = get_categories(); 
      foreach ($categories as $category) {
      echo '<input type="checkbox" name="cat_select[]" value="'.$category->cat_ID.'"> '.$category->category_nicename.'<br />';
      }
    ?>
    <button type="submit">Filter</button>
    </form>
    
    <!----- END : THIS IS FILTER FORM UNDER wp-sidebar.php -------->
    
      <aside class="<?php x_sidebar_class(); ?>" role="complementary">
        <?php if ( get_option( 'ups_sidebars' ) != array() ) : ?>
          <?php dynamic_sidebar( apply_filters( 'ups_sidebar', 'sidebar-main' ) ); ?>
        <?php else : ?>
          <?php dynamic_sidebar( 'sidebar-main' ); ?>
        <?php endif; ?>
      </aside>
    
    <?php endif; ?>

    Cheers!