Archived topic
Problem with custom pagination
5 replies · Started by George on February 5, 2023
On the attached site, I have set up the archives with the following way. For each archive, there is GP header element that has two query loops in it. They are both set to Inherit query from template.
The left loop has a class of main-cat-post and the one next to it has a class of secondary-cat-posts. I've assigned those classes to the loop's grid container.
I am offsetting the loops like so and it works fine:
//Main category post
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
// Check your logic to apply the filtering only to specific loop
if (! empty( $attributes['className'] ) && strpos( $attributes['className'], 'main-cat-post' ) !== false && ! is_admin() ) {
return array_merge( $query_args, array(
'posts_per_page' => 1,
'offset' => 0,
) );
}
return $query_args;
}, 10, 2 );
//Secondary category posts
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
// Check your logic to apply the filtering only to specific loop
if (! empty( $attributes['className'] ) && strpos( $attributes['className'], 'secondary-cat-posts' ) !== false && ! is_admin() ) {
return array_merge( $query_args, array(
'posts_per_page' => 3,
'offset' => 1,
) );
}
return $query_args;
}, 10, 2 );
Of course, I now need to offset the posts in the main content. I use a content template with the following code:
//Offset category archive posts
add_action('pre_get_posts', 'myprefix_query_offset', 1 );
function myprefix_query_offset(&$query) {
// Before anything else, make sure this is the right query and not in the admin side.
if ( ! is_admin() && $query->is_main_query() && is_category() ) {
//First, define your desired offset...
$offset = 4;
//Next, determine how many posts per page you want (we'll use WordPress's settings)
$ppp = get_option('posts_per_page');
//Next, detect and handle pagination...
if ( get_query_var('paged') ) {
//Manually determine page query offset (offset + current page (minus one) x posts per page)
$page_offset = $offset + ( (get_query_var('paged')-1) * $ppp );
//Apply adjust page offset
$query->set('offset', $page_offset );
} else {
//This is the first page. Just use the offset...
$query->set('offset',$offset);
}
}
}
add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );
function myprefix_adjust_offset_pagination($found_posts, $query) {
//Define our offset again...
$offset = 4;
//Ensure we're modifying the right query object...
if ( !is_category() && $query->is_main_query() ) {
//Reduce WordPress's found_posts count by the offset...
return $found_posts - $offset;
}
return $found_posts;
}
I've also set up an Archive Pagination element with Previous/Next buttons.
Everything works fine, apart from the Reviews archive. The NEXT button goes up to page 3 of posts that contains no posts and I get a "Page no found" error. Blog pages show at most setting is set to 8 posts per page.
I am not sure what could be going wrong, it only happens to that particular archive. Basically, on page 2 of the Reviews archive, the NEXT button at the bottom should not display but it does.
You can check the SAVE MONEY category where it works fine, on last page (3), the NEXT button is not there.
Hi George,
that i believe is related to this issue:
https://github.com/tomusborne/generateblocks/issues/713
Which has been resolved in GB 1.7. Do you want to test that ?
Hi David, just tried it, still the same issue. Problem seems to be with the GP loop not the GB one. And it seems to be doing it just for one archive. Could be a general issue, though.
Aah sorry my bad - i saw query loop and pagination issue and jumped to conclusions :)
In your last snippet:
add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );
function myprefix_adjust_offset_pagination($found_posts, $query) {
//Define our offset again...
$offset = 4;
//Ensure we're modifying the right query object...
if ( !is_category() && $query->is_main_query() ) {
//Reduce WordPress's found_posts count by the offset...
return $found_posts - $offset;
}
return $found_posts;
}
this line: if ( !is_category() && $query->is_main_query() ) {
Why is it excluding the category ( !is_category() ) when the pre_get_posts includes the category ?
That was it! Thanks David.
You're welcome George :)