Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

how to do taxonomy filter in wordpress

jQuery(window).on("load", function() {
	  var $grid = jQuery('.port-grid1').isotope({
		itemSelector: '.grid-port1',
		percentPosition: true,
		masonry: {
		  // use outer width of grid-sizer for columnWidth
		  columnWidth: '.grid-port1'
		}
	  });

	  jQuery('.item-filter').on('click', '.gallery-btn', function () {
		var filterValue = $(this).attr('data-filter');
		jQuery('.port-grid').isotope({
		  filter: filterValue
		});
	  });
	  
	  jQuery(document).on('change','select.item-filter',function()  {
	  	var filterValue = $("option:selected",this).attr("data-filter")
		jQuery('.port-grid').isotope({
		  filter: filterValue
		});
	  });
});

 <div class="col-md-4 col-sm-4 col-xs-12">
        	<!-- ITEM FILTER -->
        	<?php 
			$post_type = 'gs_bookshowcase';
			$tax_book = 'bookshowcase_group'; 
			$term_args=array( 'hide_empty' => true);
			$tax_terms_book = get_terms($tax_book, $term_args);	
			?>
            <select class="item-filter item-filter-v2 list-inline">
                <option class="active gallery-btn" data-filter=".All">All genres</option>
                <?php	
				foreach ($tax_terms_book  as $tax_term) {
					$tax_term_id = $tax_term->term_id; 									
					?>
					<option class="gallery-btn" <?php  echo "data-filter='.bookg-$tax_term_id'"; ?>><?php echo $tax_term->name; ?></option>
				<?php
				} 
				?>	
            </select>
            <!-- //item-filter -->
        </div>
                  
                  
                  <div class="gs-row author-book-list my-5 port-grid1 port-grid All">
        <?php 
        $args = array(
            'post_type' => 'gs_bookshowcase',
            'posts_per_page'=> -1,
            'tax_query' => array(
                array(
                    'taxonomy' => 'bookshowcase_group',
                    'field'    => 'slug',
                    'terms'    => $tax->slug
                ),
            ),
        );
        $query = new WP_Query( $args );

        if ( $query->have_posts() ) : 
	        while ( $query->have_posts() ) : $query->the_post();
	        $portfolio_terms_id = wp_get_post_terms($post->ID, "bookshowcase_group", array("fields" => "ids"));
	        $gsb_author = wp_get_post_terms($post->ID, "gsb_author", array("fields" => "ids"));
	        $book_language = wp_get_post_terms($post->ID, "book_language", array("fields" => "ids"));
        	?>
                <div class="col-md-4 grid-port1 grid-port All <?php foreach ($portfolio_terms_id as $portfolio_term_id) { echo "bookg-"; echo $portfolio_term_id; echo " "; }?>  <?php foreach ($gsb_author as $gsb_author_id) { echo "author-"; echo $gsb_author_id; echo " "; }?><?php foreach ($book_language as $book_language_id) { echo "lang-"; echo $book_language_id; echo " "; }?>">
 
PREVIOUS NEXT
Tagged: #taxonomy #filter #wordpress
ADD COMMENT
Topic
Name
6+7 =