Key points
- To change what an archive, search results or the blog page shows, modify the main query with
pre_get_postsrather than adding aWP_Queryto the template. - Two checks every time:
is_admin()to leave the dashboard alone and$query->is_main_query()to touch only the page’s own query. - An offset breaks pagination unless you compensate for it, and Query Loop blocks only follow this hook when they inherit the template’s query.
On the WordPress sites we take over, one pattern comes up constantly: an archive template that ignores the posts WordPress already loaded and runs its own new WP_Query() to show 12 instead of 10, or to sort them differently. It seems to work — then page 3 returns a 404, search misses the content it should find, or the site runs two heavy queries where one would do. The pre_get_posts hook handles all of this cleanly, provided you follow a few rules. Here’s how to use it, with the most common cases and the traps we’ve run into.
Why modify the main query instead of creating another one
On every page load, WordPress parses the URL, works out a query from it (the “main query”) and fetches matching content before it even picks a template. Conditional functions (is_category(), is_search()…), the page title, pagination links and the decision to serve a 404 all rely on that query.
When a template runs its own query, the main query has already run for nothing, and pagination is still calculated from it. A concrete case: Reading settings show 10 posts per page, the archive holds 25, so WordPress sees 3 pages. The template shows 5 per page through its own WP_Query and prints links up to page 5 — pages 4 and 5 return 404, because as far as the main query is concerned they don’t exist.
pre_get_posts fires before the main query runs and lets you change its parameters. One query, consistent pagination, correct conditionals: it’s the right approach whenever you’re changing the main content of an existing page. A separate WP_Query is still the right tool for secondary content (related posts, a “latest projects” strip in the footer).
The skeleton to stick to
The hook fires for every post query: the dashboard’s, widgets’, menus’, plugins’. So your code has to start by ruling out anything that isn’t the public site’s main query.
add_action( 'pre_get_posts', function ( WP_Query $query ): void {
// 1. Never in the admin, never on a secondary query
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
// 2. Target the page with $query's methods, not the global functions
if ( $query->is_post_type_archive( 'project' ) ) {
$query->set( 'posts_per_page', 12 );
$query->set( 'orderby', [ 'menu_order' => 'ASC', 'title' => 'ASC' ] );
}
} );
Three details matter. is_admin() protects admin lists: without it, cutting an archive’s page size also cuts the “Projects” screen in the dashboard, or hides content from editors. is_main_query() leaves secondary queries untouched. And you use the object’s methods ($query->is_post_type_archive()), not the global functions: at this stage, the globals read the global query, which isn’t necessarily the one you were handed.
Put this code in a small custom plugin or a mu-plugin rather than the functions.php of a theme you don’t control: it describes how the site works, not how it looks.
The most common cases
Here are the adjustments we set up most often, grouped together:
add_action( 'pre_get_posts', function ( WP_Query $query ): void {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
// Blog page: hide the "Company news" category
if ( $query->is_home() ) {
$internal = get_category_by_slug( 'company-news' );
if ( $internal ) {
$query->set( 'category__not_in', [ $internal->term_id ] );
}
}
// Search: posts and projects, no pages
if ( $query->is_search() ) {
$query->set( 'post_type', [ 'post', 'project' ] );
}
// Events: upcoming only, soonest first
if ( $query->is_post_type_archive( 'event' ) ) {
$query->set( 'meta_key', 'event_date' ); // stored as Y-m-d
$query->set( 'orderby', 'meta_value' );
$query->set( 'order', 'ASC' );
$query->set( 'meta_query', [ [
'key' => 'event_date',
'value' => wp_date( 'Y-m-d' ),
'compare' => '>=',
'type' => 'DATE',
] ] );
}
} );
On the blog page, is_home() means the posts listing, whether or not the front page is static; for the static front page itself, use is_front_page().
For events, the storage format decides everything. A date saved as Y-m-d sorts correctly as a string; a date saved as d/m/Y sorts badly and can’t be compared. If you use ACF, its date picker stores Ymd (no dashes), so pass wp_date( 'Ymd' ) as the value instead.
The traps: offset, pagination and blocks
The offset parameter (skip the first N results) disables WordPress’s native pagination, which is itself built on an offset. The classic case is a featured post shown separately at the top of the blog. You then have to compute the offset yourself and correct the total number of results:
// Blog page: the latest post is shown separately as a featured post,
// the paginated list starts from the next one.
add_action( 'pre_get_posts', function ( WP_Query $query ): void {
if ( is_admin() || ! $query->is_main_query() || ! $query->is_home() ) {
return;
}
$per_page = (int) get_option( 'posts_per_page' );
$page = max( 1, (int) $query->get( 'paged' ) );
$query->set( 'offset', 1 + ( $page - 1 ) * $per_page );
} );
// Without this correction, the page count is wrong.
add_filter( 'found_posts', function ( int $total, WP_Query $query ): int {
if ( ! is_admin() && $query->is_main_query() && $query->is_home() ) {
return max( 0, $total - 1 );
}
return $total;
}, 10, 2 );
Another frequent trap: block themes. A Query Loop block set to “inherit query from template” uses the main query, so your pre_get_posts settings apply. A custom Query Loop runs its own query and ignores them; to change it in PHP, use the query_loop_block_query_vars filter.
Finally, the REST API doesn’t use the main query: changing an archive with pre_get_posts won’t change what /wp-json/wp/v2/posts returns. For the API, the dedicated filter is rest_{type}_query (for example rest_post_query).
Check what WordPress actually runs
When a setting “doesn’t work”, the cause is almost always a condition that doesn’t match the page, or other code (theme, plugin) overriding yours at a later priority. The Query Monitor plugin shows the main query, its parameters and the template in use; it’s the first tool we open. Without it, in development:
// Temporary, development only: log the page's SQL query
add_action( 'wp', function (): void {
if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
global $wp_query;
error_log( $wp_query->request );
error_log( 'Found: ' . $wp_query->found_posts . ' / pages: ' . $wp_query->max_num_pages );
}
} );
If your change shows up in the SQL but something else overrides it, give your add_action a later priority (20, say) and search the codebase for other pre_get_posts callbacks. And clear the page cache after each test: on a live site, it’s the most mundane reason a change appears to do nothing.
FAQ
Why do my pre_get_posts changes affect the admin?
Because the hook also runs for dashboard lists. Always start the callback with if ( is_admin() || ! $query->is_main_query() ) return;. If you do want to change an admin list, do it in a separate callback, conditioned on is_admin() and the relevant screen.
Can I use pre_get_posts for a page built with a custom template?
For a static page, the main query fetches the page itself, not a list of posts, so altering it makes no sense. There, a WP_Query in the template is the right answer, with its own pagination. If the list is the main content, a post type archive is often the better fit.
Do I need wp_reset_postdata() after pre_get_posts?
No. wp_reset_postdata() is for after a loop over a secondary WP_Query. With pre_get_posts you change the main query before it runs, and the template’s standard loop uses it as usual.
A WordPress site whose archives, search or pagination behave strangely? We rework your theme and plugin code so it shows the right content with a single query and no side effects.



