AJAX Breaking Offset Argument In WP Query

I have been using AJAX on my website successfully and for my category.php page I need to use the offset argument. Unfortunately, AJAX creates complications with offset and I get the same posts looping over and over. I consulted the WordPress codex but I couldn’t not figure out how to apply a solution to my particular WP_Query. Anyone that can help I’d be grateful, I can provide more code if necessary as well. Thanks!

category.php

<?php
$category = get_category( get_query_var( ‘cat’ ) );
$cat_id = $category->cat_ID;
$current_page = max( 1, get_query_var( ‘paged’ ) );
$the_query = new WP_Query( array(
‘category__in’ => array($cat_id),
‘post_type’ => ‘post’,
‘posts_per_page’ => 5,
‘paged’ => $current_page,
) );

$_SESSION[‘count’] = 1;
wp_localize_script( ‘my_loadmore’, ‘misha_loadmore_params’, array(
‘ajaxurl’ => admin_url( ‘admin-ajax.php’, ‘relative’ ),
‘posts’ => json_encode( $the_query->query_vars ),
‘current_page’ => $current_page,
‘max_page’ => $the_query->max_num_pages
) );
?>

<div id=”main” class=”container feed-container”>
<div class=”row mx-auto align-items-center post”>
<?php if ($the_query->have_posts()) : ?>
<?php $count = 0; ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); get_template_part( ‘parts/content’, get_post_format() ); ?>
<?php $count++;

$_SESSION[‘count’]=$_SESSION[‘count’]+1;
?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
</div><!– END ROW –>
</div><!– END CONTAINER –>

Read more here:: AJAX Breaking Offset Argument In WP Query

Leave a Reply

Your email address will not be published. Required fields are marked *