- This topic has 6 replies, 2 voices, and was last updated 3 years, 2 months ago by
David.
-
AuthorPosts
-
January 10, 2023 at 2:38 am #2489835
George
I have a hero GP element displayed in category archives. In it, I have two query blocks with two different classes in their grids. I have set them to inherit from template so that they display posts from the current category and I have also modified their queries like this:
//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, ) ); } 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() ) { // Merge the current $query_args which contains arguments defined in the editor with your ordering arguments return array_merge( $query_args, array( 'posts_per_page' => 3, 'offset' => 1, ) ); } return $query_args; }, 10, 2 );As you can see, I am modifying the first query to display just one post (the first post) and the second query to display 3 posts but also offset by one post.
I am also displaying posts in the main content area. Those are posts are currently being displayed without a content template, settings are set in the Customizer. I am offsetting those posts by 4 so that I don’t get any overlap from the posts in the hero area.
//Offset category archive posts function gm_offset_category_archive_posts( $query ) { if ( is_category() && $query->is_main_query() ) { $query->set( 'offset', 4 ); } } add_action( 'pre_get_posts', 'gm_offset_category_archive_posts' );The problem I am having is that the above
pre_get_postsaction affects the query in the hero section as well. Is there a way to exclude the hero query loops from thepre_get_postsaction?January 10, 2023 at 3:46 am #2489908George
Hold on… Setting
'offset' => 0, on the first query seems to have solved the problem!January 10, 2023 at 4:05 am #2489934George
I have an issue with the Load more functionality though, in the archive! It loads more posts without taking into account the offset set in the loops. Wondering if there is a filter for that…
January 10, 2023 at 5:45 am #2490024David
StaffCustomer SupportHi George,
see here it explains how to handle offsets with pagination:
https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination
January 10, 2023 at 6:04 am #2490057George
Hmmm, on it now, it doesn’t seem to work. Are the GB query loops I am displaying in the hero considered as main queries?
January 10, 2023 at 10:38 am #2490503George
Never mind, got it to work, here is the full 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; }Thanks David!
January 11, 2023 at 3:53 am #2491179David
StaffCustomer SupportGlad to hear that George !
-
AuthorPosts
- You must be logged in to reply to this topic.