I have a list checkbox categories, which when checked, use WordPress’s admin-ajax.php to filter posts to the selected category.
All is working great, however, I am struggling to get the updated pagination. I’m using query_posts as follows:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args= array
(
'post_type'=>'course',
'paged'=>$paged,
'posts_per_page'=>3,
'tax_query' => array(
array(
'taxonomy' => 'subject',
'field' => 'slug',
'terms' => $tags_from_group
)
)
);
$posts=query_posts( $args);
I then pass the $posts back as data, but I don’t know how to access the updated pagination..
I was thinking something along these lines:
$pagedposts=array();
$pagedposts['posts']=$posts;
$pagedposts['pagination']=???
So $pagedposts[‘pagination’] would be the data needed to update the old pagi after the ajax call is successful..
I found someone else had this problem here, but there is no solution there…
In the end I found that it is not possible to update the pagination. The way I loaded the next lot of posts was by setting a post offset, and using a load more button.
So in your ajax call where you are loading more posts, you need to pass over an offset value:
Because this offset value will always change, you need to store the value in a variable. Above, I have used ‘postoffset’
The way you need to determine the value of the offset is simple. You can just use jquery to count how many posts are currently on the page:
Where .hentry is the name of my post class.
Once you receive the post offset in your php script, you can set it like so:
and then plug that into your query_args (as well as determining the offset above, I have added a ‘posts_per_page’ option which lets me clearly see the extra 9 posts loaded, and my post offset increasing by 9 each time):
To configure your load more button instead of using the pagination, you can just put a div or picture on your page, and give it an ID – for e.g.:
and the jquery: