In WooCommerce, you can use the posts_search filter to modify the query that retrieves products for the search page and exclude variation products.

Here is an example of how you can use the posts_search filter to hide the variation output on the search page:

Copy codeadd_filter( 'posts_search', 'hide_variations_on_search' );
function hide_variations_on_search( $search ) {
    if( is_search() && ! is_admin() ) {
        global $wpdb;
        $search .= " AND {$wpdb->prefix}posts.post_parent = 0 ";
    }
    return $search;
}

This code uses the posts_search filter to modify the query that retrieves products for the search page, and it adds a condition to the query that excludes variation products by checking the post_parent field. Variations have a post_parent that is not equal to zero, while the parent products post_parent is equal to zero.

The is_search() function is used to target only the search page, and the is_admin() function is used to exclude the admin pages.

You can also use the posts_clauses filter to exclude variations on the search page by adding a condition to the WHERE clause of the query, like this:

Copy codeadd_filter( 'posts_clauses', 'hide_variations_on_search', 10, 2 );
function hide_variations_on_search( $clauses, $query ) {
    if( is_search() && ! is_admin() ) {
        $clauses['where'] .= " AND {$query
(Visited 6 times, 1 visits today)
Was this article helpful?
YesNo
Close Search Window