filter by custom taxonomy using ajax

Hello i've followed this tutorial to filter posts by category using ajax and it was working fine, but i had to chan

Hello i've followed this tutorial to filter posts by category using ajax and it was working fine, but i had to change to a custom post type 'actus' and custom taxonomy 'categorie'. and didn't work anymore

please tell me what i do have to change .

HTML Code

<div class="filter-wrap">
        <div class="category">
            <?php $taxonomy  = 'categorie';
            $get_categories = get_terms($taxonomy, array('hide_empty' => 0)); ?>
            <select class="js-category js-select2">
                <option value="all">Filtrez par catégorie</option>
                <?php
                    if ( $get_categories ) :
                        foreach ( $get_categories as $cat ) :
                    ?>
                    <option value="<?php echo $cat->term_id; ?>">
                        <?php echo $cat->name; ?>
                    </option>
                    <?php endforeach; 
                        endif;
                    ?>
            </select>
        </div>
    </div> 

Then

<div class="filtered-posts">
  <?php

  if ( have_posts() ) :
     while ( have_posts() ) : the_post();
        get_template_part( 'template-parts/content-post' );
     endwhile;
   endif;
 ?>
</div>

Javascript Code

jQuery(document).ready(function($){
jQuery( ".js-category, .js-date" ).on( "change", function() {
    var category = $( '.js-category' ).val();
    var date = $( '.js-date' ).val()

    data = {
        'action': 'filterposts',
        'category': category,
        'date': date
    };

    $.ajax({
        url : ajaxurl,
        data : data,
        type : 'POST',
        beforeSend : function ( xhr ) {
            $('.filtered-posts').html( 'Loading...' );
            $('.js-category').attr( 'disabled', 'disabled' );
            $('.js-date').attr( 'disabled', 'disabled' );
        },
        success : function( data ) {
            if ( data ) {
                $('.filtered-posts').html( data.posts );

                $('.js-category').removeAttr('disabled');
                $('.js-date').removeAttr('disabled');
            } else {
                $('.filtered-posts').html( 'No posts found.' );
            }
        }
    });
});

});

Finally functions.php Code

function ajax_filterposts_handler() {
    $category = esc_attr( $_POST['category'] );

    $args = array(
        'post_type' => 'actus',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'order' => 'DESC',
        'paged' => 1,
        'tax_query' => array(
            'taxonomy' => 'categorie',
            'hide_empty' => 0,
            'field' => 'id',
            'terms' => $category
        )
    );

    if ( $category != 'all' )
        $args['cat'] = $category;




    $posts = '<div class="nopostfound">no post founds.</div>';

    $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( 'template-parts/content' );

        endwhile;

    $posts = ob_get_clean();



    }

    // Reset Post Data
    wp_reset_postdata();



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

    wp_send_json($return);
}
add_action( 'wp_ajax_filterposts', 'ajax_filterposts_handler' );
add_action( 'wp_ajax_nopriv_filterposts', 'ajax_filterposts_handler' );

Resulst " no post founds ".

what do i have to change to make this work ?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745341932a4623365.html

相关推荐

  • filter by custom taxonomy using ajax

    Hello i've followed this tutorial to filter posts by category using ajax and it was working fine, but i had to chan

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信