Limit your site search to blog posts, so pages like Cart, Checkout or Thank You don't clutter the results.
add_action( 'pre_get_posts', function ( WP_Query $query ): void {
if ( is_admin() || ! $query->is_main_query() || ! $query->is_search() ) {
return;
}
// Respect a search form that asks for a specific post type, e.g. ?post_type=product.
if ( ! $query->get( 'post_type' ) ) {
$query->set( 'post_type', [ 'post' ] );
}
} );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
WordPress search covers posts and pages by default. On most sites that means utility pages, like Cart, My Account or Privacy Policy, show up for common words. This snippet keeps search to blog posts.
pre_get_posts changes the main search query before it runs, setting post_type to posts only. If a search form already asks for a post type, such as a WooCommerce product search, that’s left alone.
[ 'post', 'product' ].Search your site for a word that appears on a page, such as “privacy”. Only posts should be listed.