Created
May 31, 2024 10:35
-
-
Save raftaar1191/0ec06e69ffc661e25a7899154b9025c9 to your computer and use it in GitHub Desktop.
Limit search results to WooCommerce Products only in frontend
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Only allow the product CPT to be search on the golbal search page | |
* | |
* @param WP_Query $query The WP_Query instance (passed by reference). | |
*/ | |
function tometal_filter_search_only_products( $query ) { | |
if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) { | |
$query->set( 'post_type', 'product' ); | |
} | |
} | |
add_action( 'pre_get_posts', 'tometal_filter_search_only_products' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment