0

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 ?

  • When you say "i had to change to a custom..." does that mean that you just changed the code to reference the new post type and category? If so, I'm wondering if there are actually posts that meet those criteria. Before troubleshooting all the code above, it might be best to confirm that there is actually a post that matches. – Justin Waulters Jul 6 at 3:49
  • Yes, there is some posts (cpt actus) with categories (taxonomy categorie). – Abder Jul 6 at 11:49
  • i fixed it. i just add this code : – Abder Jul 6 at 17:44
  • args = array( 'post_type' => 'actus', 'post_status' => 'publish', 'posts_per_page' => -1, 'order' => 'DESC', 'paged' => 1, ); if( $category != 'all' ) $args['tax_query'] = array( array( 'taxonomy' => 'categorie', 'field' => 'id', 'terms' => $_POST['category'] ) ); – Abder Jul 6 at 17:45

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.