I have a WooCommerce store for makeup, and we need to make it so that variation names are searchable, since people will often be looking for a specific shade name. I’m using this bit of code to make the product variations searchable:
add_action( 'pre_get_posts', 'search_woocommerce_product_variations' );
function search_woocommerce_product_variations( $query ) {
if( ! is_admin() && is_search() && $query->is_main_query() ) {
$query->set( 'post_type', array( 'post', 'page', 'product', 'product_variation' ) );
}
}
Edited to add: I’ve already tried adding this in, with no changes in the search results:
$query->set('post_status', array('publish'));
The problem is that when searching on the name of a variation it’s pulling up products that do that have that search term in the variation name, but are currently set to Draft status. How can I prevent that from happening?
2
Answers
Tell the query which post_status your looking for: