0

I have created an Ajax sorting feature that filters a custom post type "Music" and its all separated by Genres and Tags. The sorting works fine until. I deselect all options. I've tried to add "all" as the default and added in the JS that if the genre is "all" to include all the posts. Every thing else works as desired but I need to be able to reset the tax query or include all.

Here is that JavaScript:

jQuery(document).ready(function($){  
        
    var bgenre = $(":checkbox[name=bgenre]");
    var btags = $(":checkbox[name=btags]");
    
    jQuery.merge( bgenre, btags ).on( "change", function() {
        
    var genre;
    var tags;       
    var genrearray = [];
        var tagsarray = [];
        
        
        $(":checkbox[name=bgenre]:checked").each(function() { 
            genrearray.push($(this).val());
            
            //genre = genrearray.join();    
            genre = genrearray;         
        });        
        
        $(":checkbox[name=btags]:checked").each(function() { 
            tagsarray.push($(this).val()); 
            tags = tagsarray.join();            
        });         
    //  var tags = tagsarray.join();
    
    if (genrearray.length < 1) {
        genre = 'all';
    }

        data = {
            'action': 'filterlist',
            'genre': genre,
            'tags': tags
            
        };
 
        $.ajax({
            url : ajaxurl,
            data : data,
            type : 'POST',
            beforeSend : function ( xhr ) {

                $('.bplaylist').html( 'Loading...' );
                $('.js-Genre').attr( 'disabled', 'disabled' );
                
            },
            success : function( data ) {
                if ( data ) {                                   
                    $('.bplaylist').html( data.posts );
 
                    $('.js-Genre').removeAttr('disabled');
                    $( '.js-Tags' ).removeAttr('disabled');
                    
                } else {
                    $('.bplaylist').html( 'No posts found.' );
                }
            }
        });
    })
    });

and here is the php part

function ajax_filterlist_handler() {           
           //$tags = esc_attr( $_POST['tags'] );
           $genre = $_POST['genre']; 
           $tags = $_POST['tags'];             
        

        $args = array(
            'post_type' => 'download',
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'orderby' => 'date',
            'order' => 'DESC',
            'tax_query' => array(),
            'relation' => IN
        );
            


        if ( isset( $_POST['genre']) && $_POST['genre'] !== 'all'   ) {
        $args['tax_query'][] = array(
            'taxonomy' => 'genre',
            'field' => 'slug',
            'terms' => $genre
         ); }

         if ( isset( $_POST['genre']) && $_POST['genre'] === 'all'   ) {
         $args['tax_query'][] = array(
            'taxonomy' => 'genre'         
                  
            );
       
         }
    if ( !isset( $_POST['genre']) ) {
         $args['tax_query'][] = array( 
            );
       
         }
   
         
         if( isset( $_POST['tags'] ) ) {
        $args['tax_query'][] = array(
            'taxonomy' => 'download_tag',
            'field' => 'slug',
            'terms' => $tags
        );
    }
      

        $posts = 'No Posts Available';

        $the_query = new WP_Query( $args );
     
        if ( $the_query->have_posts() ) :
            ob_start();

            while ( $the_query->have_posts() ) : $the_query->the_post();
            get_template_part( '/custom/template-parts/trackitem' );
            endwhile;

            $posts = ob_get_clean();
        endif;

        $return = array(
            'posts' => $posts
        );

        wp_send_json($return);
    }
    add_action( 'wp_ajax_filterlist', 'ajax_filterlist_handler' );
    add_action( 'wp_ajax_nopriv_filterlist', 'ajax_filterlist_handler' );   

Sorry if the code is jumbled mess at this point. I was following a tutorial and after I got it working. I kinda went on my own to try to figure out the reset and/or all option.

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.