Skip to content

Instantly share code, notes, and snippets.

@braddalton
Created November 12, 2023 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save braddalton/2a4523a02e2ee398c7beb5089c0468a8 to your computer and use it in GitHub Desktop.
Save braddalton/2a4523a02e2ee398c7beb5089c0468a8 to your computer and use it in GitHub Desktop.
Exclude products from search results in WordPress
function exclude_products_from_search($query) {
if (is_admin() || !$query->is_main_query()) {
return;
}
if ($query->is_search) {
$query->set('post_type', array('post', 'page')); // Exclude products from search results
}
}
add_action('pre_get_posts', 'exclude_products_from_search');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment