Support » Developing with WordPress » Exclude categories by filtering with isotope.js

  • j_spaceman

    (@nothinghalosix)


    hi,

    i hope somebody can help me. I have an custom post type (employees) with categories ( office vienna, office berlin, …). They are displayed at the archive-employees.php template. I display the categories to filter (with isotope.js) in a dropdown and i will exclude some categories (uncategorized, …)

    see my code below

    <div class="flex flex-wrap">
            <div class="lg:w-2/3 pr-8 sm:w-full">
                <p><?php _e('Specialists from all around the world form a multicultural team at. As a Destination Management Company we speak your language and know how your customers tick. Our experienced staff members are more than happy to fulfil your customers needs.', 'nothing')  ?></p> 
            </div>
            <div class="lg:w-1/3">
                <div class="relative">
                    <?php
                    $customPostType = 'employees';
                    $customTax = 'category';
                    $wp_query = new WP_Query();  
                    $wp_query->query(array(
                        'post_type'=> $customPostType,
                        'posts_per_page' => -1,
                        'paged' => $paged,
                    ));
           
                    if ($wp_query->have_posts()) :  
                        // Create Filter buttons from all Categories that have posts            
                        // create an array
                        $filterLinks = array();
                        // an empty array to add to our previous array
                        $newarray = array();
                        // Starter "Show All' Array
                        $starter = array('name'=>'show all','slug'=>'*');
                        // put it into our array
                        $filterLinks[] = $starter;
                      
                        $taxterms = get_terms( $customTax );
                        // loop through terms if not empty
                        if ( ! empty( $taxterms ) && ! is_wp_error( $taxterms ) ){
                        
                            foreach ( $taxterms as $taxterm ) {
                                // show all is a little different, so we add a "." to the others
                                $datafilter = '.' . $taxterm->slug;
                                // create some 
                                    $newarray = array(
                                        'name' => $taxterm->name,
                                        'slug' => $datafilter,
                                    );
    
                                    // load it up 
                                    $filterLinks[] = $newarray;        
                            } 
                        } 
    
                        // print_r ($newarray);         
                    echo '<div id="filters" class="button-group"><select class="w-full md:w-full border h-10 text-black-link px-2 appearance-none">' ."\n";
                        foreach ($filterLinks as $links) {
                            echo '<option class="button" data-filter="' . $links['slug'] . '">' . $links['name'] . '</option>'."\n";
                        }
                    echo '</select></div><!-- isotope filters -->';
                
                    ?>
                    <div class="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-gray-700">
                        <svg class="fill-current h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z"></path></svg>
                    </div>
                </div>
            </div>
        </div>
        <div id="container" class="flex flex-wrap -mx-2">
            <?php while ($wp_query->have_posts()) : $wp_query->the_post(); 
            // Get the terms with each post
            $terms = get_the_terms( $post->ID, $customTax );
                                    
            if ( $terms && ! is_wp_error( $terms ) ) : 
    
                $cat_links = array();
    
                foreach ( $terms as $term ) {
                    $cat_links[] = $term->slug;
                }
                                    
                $mycats = join( " ", $cat_links );
            
            endif; 
            ?>
            <div class="item newsblock w-1/4 p-2 employee <?php echo $mycats; ?>">
                <a href="<?php the_permalink(); ?>">
                    <div class="hover">
                        <section>
                            <div class="item-img border-secondary hover:bg-secondary">
                                <?php $team_img = get_the_post_thumbnail_url(); ?>
                                <img src="<?= $team_img; ?>" alt="" class="image" style="width:100%">
                                <div class="middle">
                                    <div class="team text-right">
                                        <span class="text-xs text-white"><?php the_field('office'); ?></span>
                                    </div>
                                    <div class="language text-right">
                                        <span class="text-xs text-white"><?php the_field('languages'); ?></span>
                                    </div>
                                    <span class="text-3xl block text-white"><?= the_field('greetings'); ?></span>
                                    <div class="text">
                                        <span class="text-2xl text-right font-bold my-4 block text-white"><?= the_title(); ?></span>
                                    </div>
                                    <hr class="border border-t-white">
                                    <div class="text-right">
                                        <span class="text-right text-xs text-white"><?= the_field('position'); ?></span>
                                    </div>
                                </div>
                            </div> 
                        </section> 
                        
                    </div> 
                </a>
            </div>
            <?php endwhile; ?>
        </div>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Joy

    (@joyously)

    What is the question?

    Moderator bcworkz

    (@bcworkz)

    Categories or custom taxonomy terms? I’ll show you the taxonomy terms, which will work for categories as well. Use the tax_query argument, passing a structured array that defines your desired query logic. You can specify which to include, not include, the logic to use between multiple terms, etc. Very complex criteria can be specified. See https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters

    You can pass your query arguments to the class as it’s instantiated. By waiting to use $wp_query->query you are actually making two queries, discarding one. A bit wasteful.

    j_spaceman

    (@nothinghalosix)

    hi bcworks,

    thank you for your respond. i’m not sure whats you mean to use a tax_query argument and the query logic.

    j_spaceman

    (@nothinghalosix)

    i tried this

    
    $customTax = 'category';
    $wp_query->query(array(
     'post_type'=> 'employees',
     'posts_per_page' => -1,
     'tax_query' => array(
        'category__not_in' => 3,
      )
    ));

    but it’s not working

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.