If the WooCommerce product search is not working on your WordPress website, it can be frustrating for both you and your users. One possible solution is to modify the search query to include product post types. Here’s an example code snippet that you can add to your theme’s functions.php
file:
/**
* Modify search query to include product post types.
*
* @param WP_Query $query The WP_Query instance.
*/
function wpsnippets_modify_product_search_query( $query ) {
if ( $query->is_search && ! is_admin() && is_post_type_archive( 'product' ) ) {
$query->set( 'post_type', array( 'post', 'page', 'product' ) );
}
}
add_action( 'pre_get_posts', 'wpsnippets_modify_product_search_query' );
This code snippet hooks into the pre_get_posts
action and modifies the search query to include the product
post type when searching. It checks if the current query is a search query, not in the admin area, and on a product archive page. If these conditions are met, it sets the post_type
parameter to include post
, page
, and product
post types.
By adding this code snippet, the WooCommerce product search should start including products in the search results.
Examples
Example #1: Troubleshooting WooCommerce product search not working
This example demonstrates how to troubleshoot and fix issues with the WooCommerce product search functionality not working properly.
/**
* Disable WooCommerce product search query caching.
*/
function wpsnippets_disable_product_search_caching( $query ) {
if ( is_search() && is_main_query() && ! is_admin() ) {
$query->set( 'suppress_filters', true );
}
}
add_action( 'pre_get_posts', 'wpsnippets_disable_product_search_caching' );
This code snippet disables the caching of WooCommerce product search queries, ensuring that the search results are always up to date. It hooks into the pre_get_posts
action and modifies the query to suppress filters specifically for the main search query on the front-end.
Example #2: Rebuilding WooCommerce product search index
This example demonstrates how to rebuild the WooCommerce product search index, which can help resolve issues with the search functionality not returning accurate results.
/**
* Rebuild WooCommerce product search index.
*/
function wpsnippets_rebuild_product_search_index() {
global $wpdb;
$wpdb->query( "DELETE FROM {$wpdb->prefix}woocommerce_termmeta WHERE meta_key = '_product_count'" );
$wpdb->query( "DELETE FROM {$wpdb->prefix}woocommerce_termmeta WHERE meta_key = '_product_count_manual'" );
$wpdb->query( "DELETE FROM {$wpdb->prefix}woocommerce_termmeta WHERE meta_key = '_product_count_auto'" );
$terms = get_terms( 'product_cat', array( 'hide_empty' => false ) );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
delete_transient( 'wc_term_counts_' . $term->term_id );
}
}
}
add_action( 'wp_loaded', 'wpsnippets_rebuild_product_search_index' );
This code snippet rebuilds the WooCommerce product search index by deleting certain term meta entries related to product counts and clearing transients for product categories. It hooks into the wp_loaded
action to ensure that the index is rebuilt when the WordPress environment is fully loaded.
Example #3: Customizing WooCommerce product search query
This example demonstrates how to customize the WooCommerce product search query to include additional search parameters or modify the default behavior.
/**
* Customize WooCommerce product search query.
*/
function wpsnippets_customize_product_search_query( $query ) {
if ( is_search() && is_main_query() && ! is_admin() ) {
$query->set( 'post_type', array( 'product', 'product_variation' ) );
$query->set( 'meta_query', array(
array(
'key' => '_sku',
'value' => get_search_query(),
'compare' => 'LIKE',
),
) );
}
}
add_action( 'pre_get_posts', 'wpsnippets_customize_product_search_query' );
This code snippet customizes the WooCommerce product search query to include both products and product variations in the search results. It also adds a meta query to search for the entered search query within the product SKU (Stock Keeping Unit) field. The customization is applied to the main search query on the front-end using the pre_get_posts
action.