Site icon Hip-Hop Website Design and Development

Ajax load more posts with multiple tax query

I am trying load my posts based on which tax query user is selecting in that moment.
Currently it only works for my posts where it starts from offset = 0 and continue until has no more posts. The problem is .. when they change taxonomies wp brings me back duplicate posts forever. I tried to set offset to 0 whenever an select option is sent back to ajax..
But it works only for the first ‘load’ of the posts , the next one will just duplicate them.

So I have these selects that works together each.

posts.php

  <div class="product-form-row">
     <label for="category-select">Categorie Vehicul:</label>
   <select name="category-select" id="only-this" class="primary-select taxonomy categorie_principala only-one ">
                        <option value="default" selected disabled>Selecteaza</option>
                                            
                            <?php if(taxonomy_exists('categorie_produse_leasing')):
                              
                                $toate_cat = get_terms('categorie_produse_leasing', array('hide_empty' => 0));
                                $second_level = array_filter($toate_cat, function ($t) {
                                
                                    return $t->parent != 0 && get_term($t->parent, 'categorie_produse_leasing')->parent == 0;
                                });
                              
                                if ($second_level):                                                                       
                                    foreach($second_level as $single_cat): ?>
                                        <option value="<?php echo $single_cat->term_id; ?>"><?php echo $single_cat->name; ?></option>
                                    <?php endforeach; 
                                endif;
                            endif;?>

                    </select>
  </div>

            <div class="product-form-row">
                <label for="category-select">
                    Tip Vehicul:
                </label>
                <select name="category-select" class="primary-select taxonomy only-one" id="tip_vehicul_expand">
                    <option value="default" selected>Selecteaza</option>
                </select>
            </div>
            
            <div class="product-form-row">
                <label for="category-select">
                    Marca:
                </label>
                <select name="category-select" class="primary-select taxonomy marca-select only-one" id="marca_expand">
                    <option value="default" selected>Selecteaza</option>
                </select>
            </div>
            
            <div class="product-form-row model-body">
                <label for="category-select">
                    Model:
                </label>
                <select name="category-select" class="primary-select taxonomy model-select only-one" id="model_expand">
                    <option selected value="default">Selecteaza</option> 
                </select>
                    
            </div>

<script>
 (function($) {       
        
                    var ajaxUrl = "<?php echo admin_url('admin-ajax.php')?>";
                    var page = 1; // What page we are on.
                    var ppp = 4; // Post per page

                    $(".load-more-posts").on("click",function(){
                        $(".load-more-posts").attr("disabled",true); 
                        
                        $('.loading-div').removeClass('off');
                        $('.product-flow-panel').addClass('off');
                        
                    
                            var offset = (page * ppp) +1;
                            var taxonomy = $('.categorie_principala').val();
                            var tip_client = $('#_tip_client').val();
                            var tip_vehicul = $('#tip_vehicul_expand').val();
                            var marca = $('#marca_expand').val();
                            var model = $('#model_expand').val();
                            var combustibil = $('#_combustibil').val();
                            var nou_rulat = $('#_nou_rulat').val();
                            var order = $('#_order').val();
                            
                            
// if(taxonomy != null || tip_client != 'default' || tip_vehicul != 'default' || marca != 'default' || model != 'default' || combustibil != 'default' || nou_rulat != 'default' || order != 'default'){
                            
                        //     if(offset != 0){
                        //         offset = 0;
                        //     }else{
                        //        offset = (page * ppp) +1;
                        //     }
                        
                            
                        // }
                            
                
                        $.post(ajaxUrl, {
                            action:"more_post_ajax",
                            offset: offset,
                            ppp: ppp,
                            paged: <?php echo $current_page; ?>,
                            taxonomy: taxonomy,
                            tip_client: tip_client,
                            tip_vehicul: tip_vehicul,
                            marca: marca,
                            model: model,
                            combustibil: combustibil,
                            nou_rulat: nou_rulat,
                            order: order
                            
                        }).success(function(posts){
                            
                            console.log(posts);
                            
                            if(posts != 0){
                                
                                setTimeout(() => {
                                $('.loading-div').addClass('off');              
                                }, 300);
                                
                                setTimeout(() => {
                                $('.product-flow-panel').removeClass('off'); 
                                $(".post_container").append(posts); 
                                $(".load-more-posts").attr("disabled",false);
                                }, 500);
                            }
                            else{
                                setTimeout(() => {
                                $('.loading-div').addClass('off');              
                                }, 300);
                                
                                setTimeout(() => {
                                $('.product-flow-panel').removeClass('off'); 
                                alert('Nu au fost gasite alte oferte.');
                                $(".load-more-posts").attr("disabled",false);
                                });
                            }
                        
                           
                        });

                    });
                    
                                           

                })(jQuery);    
</script>

** load-more.php **

function more_post_ajax(){

    $offset = $_POST["offset"];

    $ppp = $_POST["ppp"];
    
    $paged = $_POST["paged"];

    $taxonomy = $_POST["taxonomy"]; //categoria dinamica;

   $tip_client = $_POST["tip_client"];
   
   $tip_vehicul = $_POST["tip_vehicul"];
   
   $marca = $_POST["marca"];
   
   $model = $_POST["model"];
   
   $combustibil = $_POST["combustibil"];
   
   $nou_rulat = $_POST["nou_rulat"];
   
   $order = $_POST["order"];
   
    $args = array(

        'post_type' => 'produse_leasing',

        'posts_per_page' => $ppp,

        'offset' => $offset,
        
        'paged' => $paged,
        
        'orderBy' => 'date'

    );
    
    


    if ($taxonomy) {

        $array_taxonomie = array(       

            'taxonomy' => 'categorie_produse_leasing', 

            'terms'    => array($taxonomy),

            'field' => 'term_id',

            'operator' => 'IN'

        );

        // here i think i have to get the offset to be dynamically somehow

        $args['offset'] = $args['posts_per_page'];
        
        
        $tax_query[] = $array_taxonomie;

    }

    

    if ($tip_client != 'default') {

        $array_taxonomie = array(    

            'taxonomy' => 'tip_client', 

            'terms'    => array($tip_client),

            'field' => 'term_id',

            'operator' => 'IN'

        );

        $tax_query[] = $array_taxonomie;

    }

     

    if ($tip_vehicul != 'default') {

        $array_taxonomie = array(    

            'taxonomy' => 'tip_vehicul', 

            'terms'    => array($tip_vehicul),

            'field' => 'term_id',

            'operator' => 'IN'

        );

        $tax_query[] = $array_taxonomie;

    }