Skip to main content
Login Join
Snippet · PHP

Run Code Only on the Main Query

Shared by Yusuf mudagal · May 2, 2026 · @pre_get_posts

32 views
Back to Snippets

Shows a safe pre_get_posts pattern for modifying only the primary front-end query.

add_action( 'pre_get_posts', 'wpfolks_adjust_main_blog_query' );

function wpfolks_adjust_main_blog_query( $query ) {
    if ( is_admin() || ! $query->is_main_query() || ! $query->is_home() ) {
        return;
    }

    $query->set( 'posts_per_page', 12 );
}
Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

0 comments