Как совместить фильтры и пагинацию Wordpress?

Есть фильтр по категориям на ajax и обычная wordpress пагинация. Если применить фильтр, то пагинация не правильно срабатывает (отображает так же все страницы (а не из этой категории). Так же при переходе на 2 страницу сбрасывается фильтр ajax.

Очень нужна помощь

index.php:

                    <div class="blog-entry__wrap">
                    <div class="blog-entry__filter">
                        <p>Поиск по тегам</p>
                            <?php
                            echo '<form action="" method="POST" id="filter" class="swiper-container">';
                            echo '<div class="swiper-wrapper">';
                            // категории
                            global $wp;
                            $base = home_url( $wp->request ); // Gets the current page we are on.
                            if( $terms = get_terms( array( 
                                'taxonomy' => 'category', 
                                'orderby' => 'name' 
                                ) ) ) { 
                                foreach ( $terms as $term ) {
                                    echo '<input type="radio" class="custom-radio" name="categoryfilter" id="radio-' . $term->term_id . '" value="' . $term->term_id . '" />
                                    <label class="swiper-slide" for="radio-' . $term->term_id . '">' . $term->name . '</label>';
                                }
                            }
                                         
                            echo '<button class="none" id="submit">Применить фильтр</button><input type="hidden" name="action" value="myfilter"><input type="hidden" name="base" value="'. $base .'"/>';
                            echo '</div>';
                            echo '</form>';
                            ?>
                                <div class="slides-navi">
                                    <div class="button-prev"></div>
                                    <div class="button-next"></div>
                                </div>
                    </div>
                    <div class="cards cards_view-1" id="response">
                        <?php
                        if ( have_posts() ) :
                            /* Start the Loop */
                            while ( have_posts() ) :
                                the_post();

                                /*
                                * Include the Post-Type-specific template for the content.
                                * If you want to override this in a child theme, then include a file
                                * called content-___.php (where ___ is the Post Type name) and that will be used instead.
                                */
                                get_template_part( 'template-parts/content', get_post_type() );

                            endwhile;

                             ?>

                        <?php

                        else :
                            get_template_part( 'template-parts/content', 'none' );

                        endif;
                        ?>
                </div><!-- #cards -->
                <div class="blog-entry__nav">
                <?php
                    echo paginate_links(
                        array(
                            'prev_text' => __( '<div class="button-prev"></div>' ),
                            'next_text' => __( '<div class="button-next"></div>' ),
                        )
                    );
                    ?>
                </div>
            </div><!-- #wrapper -->

Подключение:

add_action( 'wp_enqueue_scripts', 'truemisha_jquery_scripts' );
 
function truemisha_jquery_scripts() {
    wp_enqueue_script( 'jquery' );
 
    wp_register_script( 'filter', get_stylesheet_directory_uri() . '/assets/js/filter.js', array( 'jquery' ), time(), true );
    wp_enqueue_script( 'filter' );
    wp_localize_script( 'filter', 'true_obj', array( 
        'ajaxurl' => admin_url( 'admin-ajax.php' ),
        ) );
 
}

functions.php:

add_action( 'wp_ajax_myfilter', 'true_filter_function' ); 
add_action( 'wp_ajax_nopriv_myfilter', 'true_filter_function' );
 
function true_filter_function(){

    $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;

    $args = array(
        'orderby' => 'date', // сортировка по дате у нас будет в любом случае (но вы можете изменить/доработать это)
        'order' => $_POST[ 'date' ], // ASC или DESC
        'post_type'      => 'post',
        'post_status'    => 'publish',
        'paged'     => $paged,
    );


    // для таксономий
    if( isset( $_POST[ 'categoryfilter' ] ) ) {
        $args[ 'tax_query' ] = array(
            array(
                'taxonomy' => 'category',
                'field' => 'id',
                'terms' => $_POST[ 'categoryfilter' ],
            )
        );
    }
 
    query_posts( $args );
 
    if ( have_posts() ) {
            while ( have_posts() ) : the_post();
            // вывод шаблона поста
            get_template_part( 'template-parts/content', get_post_type() );
        endwhile; 

    } else {
        echo 'Ничего не найдено';
    }
    die();


}

js:

jQuery( function( $ ){
    $( '#filter' ).submit(function(){
        var filter = $(this);
        var content = $('.cards');

        $.ajax({
            url : true_obj.ajaxurl, // обработчик
            data : filter.serialize(), // данные
            type : 'POST', // тип запроса
            beforeSend : function( xhr ){
                filter.find( 'button' ).text( 'Загружаю...' ); // изменяем текст кнопки
                content.addClass('loading');
            },
            success : function( data ){
                content.removeClass('loading');
                filter.find( 'button' ).text( 'Применить фильтр' ); // возвращаеи текст кнопки
                $( '#response' ).html(data);
            }
        });
        return false;
    });
});
jQuery('.custom-radio').change(function(){
    jQuery('#submit').click()
})


Ответы (0 шт):