wp query – next and prev with ajax

Question

i am trying to set up next and prev button for ajax the problem i have is that page is always 2 and can’t go over it; it seems like it increments only once and resets each i click.

$(document).on('click', '.next', function () {
     
    var successCount = $('.postbody').data("page");
     
    $.ajax({
        type: 'POST',
        url: '<?php echo admin_url('admin-ajax.php');?>',
        dataType: "html", // add data type
        data: { 

            action : 'ajaxnext_posts',
            page: parseInt(successCount),
            sub: $('.postbody').data('sub'),
        },
        success: function( response ) {
            console.log( response );
                                        successCount++;

            //alert(response);
            $( '.postbody' ).html( response ); 
            $('.all').addClass('active');
            //$('.all').removeClass('active');
            $('.sub').removeClass('active');
            $('.dub').removeClass('active');
            
            //var getsub = $('.postbody').data('sub'),
            $( '.postbody' ).attr('data-page', successCount);
            //$( '.postbody' ).attr('data-sub', '');
        }
    });

 });

and this is the function

function ajaxnext_posts() {
// Query Arguments

            $paged =  intval($_POST['page'])+1;   
        error_log($paged);
$paged = $_POST['page']+1;
error_log($paged);
$args = array(
    'post_type'     => array('anime'),
    'post_status'   => array('publish'),
    'posts_per_page'=> 20,
    'nopaging'      => false,
    'order'         => 'DESC',
    'orderby'       => 'date',
    'paged'         => ++$paged,
    'meta_key'      => 'ero_sub',
    'meta_value'    => $_POST['sub']
);
        //$paged = get_query_var("paged")?:1;
        //$paged = 1;
    //  $paged = intval($paged);
        //if ($paged < 1) $paged = 1;
        

        
// The Query
$ajaxposts = new WP_Query( $args );




$response="";

// The Query
if ( $ajaxposts->have_posts() ) {
    $response .= get_template_part('ajax-home-lastet');
    
    

    while ( $ajaxposts->have_posts() ) {
        $ajaxposts->the_post();
        $response .= get_template_part('style-3');
    }
    echo '  </div>  </div>';
} /*else {
    $response .= get_template_part('none');
}*/

exit; // leave ajax call

}

// Fire AJAX action for both logged in and non-logged in users
add_action(‘wp_ajax_ajaxnext_posts’, ‘ajaxnext_posts’);
add_action(‘wp_ajax_nopriv_ajaxnext_posts’, ‘ajaxnext_posts’);

0
Seddek Iguijji 1 week 2021-04-05T05:46:46-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse