Archived topic
Exclude queries in hero from pre_get_posts action
6 replies · Started by George on January 10, 2023
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_posts action affects the query in the hero section as well. Is there a way to exclude the hero query loops from the pre_get_posts action?
Hold on... Setting 'offset' => 0, on the first query seems to have solved the problem!
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...
Hi George,
see here it explains how to handle offsets with pagination:
https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination
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?
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!
Glad to hear that George !