0

I have an issue when using wp ajax. my WP Query for Custom Post type not working with paginate links I have change base URL but /page/2 return 404

I use Custom post type and custom taxonomy for my website

here my code:

add_action('wp_ajax_taxFilterKlussen', 'tax_klussen_filter_function'); 
add_action('wp_ajax_nopriv_taxFilterKlussen', 'tax_klussen_filter_function');
 function tax_klussen_filter_function(){
    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
    $args = array(
        'post_type' => 'klussen',
        'orderby' => 'date', // we will sort posts by date
        'order' => 'DESC', // ASC or DESC
        'posts_per_page' => 6,
        'paged' => $paged
    );

    if( isset( $_POST['keyword'] ) )
        $args['s'] = esc_attr( $_POST['keyword'] );



    // for taxonomies / categories
    if( isset( $_POST['taxfilter_check'] ) )
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'cat_klussen',
                'field' => 'id',
                'terms' => $_POST['taxfilter_check'],
                'operator' => 'IN',

            )
        );

    if( isset( $_POST['taxremove'] ) )
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'cat_klussen',
                'field' => 'name',
                'terms' => $_POST['taxremove'],
                'operator' => 'NOT IN',

            )
        );

    if( isset( $_POST['taxremove'] ) &&  isset($_POST['count'])   )
        $args = array(
        'post_type' => 'klussen',
        'orderby' => 'date', // we will sort posts by date
        'order' => 'DESC', // ASC or DESC
        'posts_per_page' => 6,
        'paged' => $paged
    );





    $query = new WP_Query( $args );

    echo '<div class="listing-grid">';
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post(); 
            $thumb = get_the_post_thumbnail_url();
            $single_cat = get_the_terms( get_the_ID(), 'cat_klussen' );
            $klussen_city = get_the_terms( get_the_ID(), 'klussen_city' );
            ?>
            <div class="listing-item" onclick="location.href='<?php the_permalink()?>'" style="cursor:pointer;">
                <div class="listing-content">                                          
                    <?php the_title( '<h3 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h3>' ); ?>
                    <div class="col-city-addres"></div>
                    <div class="bottom-content">
                        <div class="col-date-schedule">
                            <span class="icon-date"><?php the_field('klussen_date');?></span>
                            <span class="icon-schedule"><?php the_field('klussen_schedule')?></span>
                        </div>
                        <div class="col-reacties-price">
                            <span><?php the_field('klussen_reacties');?> reacties</span>
                            <span class="c-prices"> <a href="<?php echo get_permalink(); ?>" class="klussen-read-more" ><?php the_field('klussen_price')?></a></span>
                        </div>
                    </div>

                </div>
            </div>
    <?php   } ?>
    <div class="listing-pagination">
        <?php 
        $total_pages = $query->max_num_pages;
            if ($total_pages > 1){

                $current_page = max( 1, get_query_var('paged'));
                $base = trailingslashit(site_url() .'/klussen');
                echo paginate_links(array(
                    'base' => $base . '%_%',
                    'format' => '/page/%#%',
                    'current' => $current_page,
                    'total' => $total_pages,
                    'prev_next' => false,
                ));
            }
        ?>
    </div>
    <?php
    }else{
            echo 'Geen berichten gevonden';
        }
    echo '</div>';

    die();

}

before I Change bas my base url is "/wp-admin/wp-ajax.php"

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.