Following this great tutorial I've implemented Ajax pagination with scrolling: https://www.billerickson.net/infinite-scroll-in-wordpress/.
Now, I've added a second Ajax call to filter the posts by category and I would to make pagination works also for the filtered posts.
So I thought to make something like this in function filter_posts() in functions.php:
function filter_posts() {
global $wp_query;
$args['category_name'] = $_POST['passed_slug_via_ajax'];
query_posts($args);
[loop]
wp_send_json_success( $data );
};
I thought that in this way, ie using query_posts
, the current query global $wp_query;
was altered to include the $args['category_name']
filter, so when I trigger the "more posts" function it would paginate the filtered results:
// more posts" function
function more_posts() {
global $wp_query;
$args['paged'] = $_POST['passed_page_via_ajax'];
query_posts($args);
[loop]
wp_send_json_success( $data );
};
No way: the tiggered more_posts()
function doesn't apply the filter...
So I've tried to manually alter the global $wp_query;
in this way in filter_posts()
function:
function filter_posts() {
global $wp_query;
$args['category_name'] = $_POST['passed_slug_via_ajax'];
query_posts($args);
[loop]
$wp_query->set( "category_name", $_POST['passed_slug_via_ajax'] );
wp_send_json_success( $data );
};
But it doesn't work...
Maybe I didn't catch exactly the idea of how global $wp_query;
it works?
wp_localize_script()
is used to pass to JS the global $wp_query object with'query' => $wp_query->query
; then, functionbe_ajax_load_more()
get all the $args it need from this global query object