- This topic has 5 replies, 2 voices, and was last updated 3 years, 3 months ago by
David.
-
AuthorPosts
-
February 5, 2023 at 12:01 pm #2522440
George
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-postand the one next to it has a class ofsecondary-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 mostsetting 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.
February 6, 2023 at 4:24 am #2523053David
StaffCustomer SupportHi 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 ?
February 6, 2023 at 6:43 am #2523165George
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.
February 6, 2023 at 8:05 am #2523362David
StaffCustomer SupportAah 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 ?February 7, 2023 at 7:35 am #2524501George
That was it! Thanks David.
February 8, 2023 at 3:46 am #2525647David
StaffCustomer SupportYou’re welcome George 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.