Your cart is currently empty!
How to prevent WooCommerce products from being displayed when searching from the WordPress regular search widget
If you have WooCommerce installed in WordPress, all posts, pages, and products will be displayed when you search from the regular search widget. Today I will introduce a code that does not display the product and page when searching from the regular search widget. Put the following code in functions.php.
/** Remove WooCommerce products and WordPress page results from search form widget */
function mycustom_modify_search_query( $query ) {
// Make sure this isn't the admin or is the main query
if( is_admin() || ! $query->is_main_query() ) {
return;
}
// Make sure it is not a WooCommerce product search form
if( isset($_GET['post_type']) && ($_GET['post_type'] == 'product') ) {
return;
}
if( $query->is_search() ) {
$in_search_post_types = get_post_types( array( 'exclude_from_search' => false ) );
// Type of post to delete (Example: "product" and "page")
$post_types_to_remove = array( 'product', 'page' );
foreach( $post_types_to_remove as $post_type_to_remove ) {
if( is_array( $in_search_post_types ) && in_array( $post_type_to_remove, $in_search_post_types ) ) {
unset( $in_search_post_types[ $post_type_to_remove ] );
$query->set( 'post_type', $in_search_post_types );
}
}
}
}
add_action( 'pre_get_posts', 'mycustom_modify_search_query' );
Comments
Featured products
-
[WAV] Hiphop and Rap Music – Genx Beats
$7.00 or more -
[WAV] Hiphop and Rap Music Vol. 2 – Genx Beats
$7.00 or more -
[WAV] Hiphop and Rap Music Vol. 3 – Genx Beats
$7.00 or more
Leave a Reply