• DONATE to NULLED!
    Вы можете помочь Форуму и команде, поддержать финансово.
    starwanderer - модератор этого раздела будет Вам благодарен!

Помощь Пагинация в Ajax Фильтре

ranaza12

Создатель
Регистрация
11 Июл 2012
Сообщения
37
Реакции
3
Помогите добавить пагинацию в фильтр, перепробивал все методы, ничё не помогает (((
Код:
add_action('wp_ajax_ajax_filter', 'ajax_filter_callback');
add_action('wp_ajax_nopriv_ajax_filter', 'ajax_filter_callback');
function ajax_filter_callback() {
    header("Content-Type: application/json");
    $meta_query = array('relation' => 'AND');
    if(isset($_GET['cmstype'])) {
        $cmstype = sanitize_text_field( $_GET['cmstype'] );
        $meta_query[] = array(
            'key' => 'cmstype',
            'value' => $cmstype,
            'compare' => '='
        );
    }
   
    if(isset($_GET['price'])) {
        $price = sanitize_text_field( $_GET['price'] );
        $meta_query[] = array(
            'key' => 'price',
            'value' => $price,
            'compare' => '='
        );
    }
   
    if(isset($_GET['forcms'])) {
        $forcms = sanitize_text_field( $_GET['forcms'] );
        $meta_query[] = array(
            'key' => 'forcms',
            'value' => $forcms,
            'compare' => 'LIKE'
        );
    }   
   
    $tax_query = array();
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $args = array(
        'post_type' => 'shop-view',
        'paged' => $paged,
        'posts_per_page' => 4,
        'meta_query' => $meta_query,
        'tax_query' => $tax_query
       
    );
    if(isset($_GET['search'])) {
        $search = sanitize_text_field( $_GET['search'] );
        $search_query = new WP_Query( array(
            'post_type' => 'shop-view',
            'posts_per_page' => -1,
            'meta_query' => $meta_query,
            'tax_query' => $tax_query,
            'paged' => $paged,
            's' => $search
        ) );
    } else {
        $search_query = new WP_Query( $args );
    }
   
    if ( $search_query->have_posts() ) {
        $result = array();
        while ( $search_query->have_posts() ) {
            $search_query->the_post();
            $cats = strip_tags( get_the_category_list(", ") );
            $result[] = array(
                "id" => get_the_ID(),
                "title" => get_the_title(),
                "content" => get_the_content(),
                "permalink" => get_permalink(),
                "cmstype" => get_field('cmstype'),
                "forcms" => get_field('forcms'),
                "poster" => get_the_post_thumbnail_url($post->ID,$size='portgolio')
            );
        }
        wp_reset_query();
        echo json_encode($result);
    } else {
        // no posts found
    }
    wp_die();
}

JS
Код:
$.ajax({
        url : ajax_url,
        data : data,
        success : function(response) {
            mafs.find(".res").empty();
            if(response) {
                for(var i = 0 ;  i < response.length ; i++) {
var html  = "<article class='col-lg-6 pic pic-3d pic-shop por-4 hentry'>";
html += "<div class='entry-content'>";
html += "<h6>";
html += "<a href='" + response[i].permalink + "' title='" + response[i].title + "'>";
html += " " + response[i].title + " ";
html += "</a>";
html += "</h6>";
html += "<div class='thumbnail'>";
html += "<img class='pic-image' src='" + response[i].poster + "' alt='" + response[i].title + "' />";
html += "</div>";
html += "</div>";
html += "<div class='line-btn'>";
html += "<a class='bttn btshop' href='" + response[i].permalink + "'><i class='fas fa-eye'></i> Подробнее</a>";
html += "<p><i class='fas fa-hryvnia'></i></p>";
html += "</div>";
html += "</div>";
html += "</article>";
mafs.find(".res").append(html);
                }
            } else {
                var html  = "<li class='no-result'>No matching movies found. Try a different filter or search keyword</li>";
                mafs.find(".res").append(html);
            }
        }
    });

});
 
У вас тут только часть кода.
$paged - указывает какую страницу пагинации запрашивать в WP_Query.
Получать её нужно не через get_query_var(), а через $_GET параметр, как и "cmstype" и т.д.
Но для начала ж нужно добавить пагинацию в блок, где выводиться цикл записей и сделать так, чтобы она попадала в data при ajax-запросе.
 
Назад
Сверху