is_main_query() ) { return; } $is_product_archive = is_shop() || is_product_category() || is_product_tag(); $is_product_search = is_search() && $query->get( 'post_type' ) === 'product'; if ( ! ( $is_product_archive || $is_product_search ) ) { return; // Leave normal blog/page search (or any other query) untouched. } // Include product_variation alongside product. $query->set( 'post_type', array( 'product', 'product_variation' ) ); // Variations don't have a post_status of 'publish' by default in some // setups but usually inherit 'publish' — WooCommerce sets this when // the variation is enabled, so no change needed there. $existing_meta_query = (array) $query->get( 'meta_query' ); $existing_meta_query[] = array( 'relation' => 'OR', // Product posts: no representative flag exists on them, let them through. array( 'key' => '_adnvg_representative', 'compare' => 'NOT EXISTS', ), // Variation posts: only the flagged representative. array( 'key' => '_adnvg_representative', 'value' => 1, ), ); $existing_meta_query[] = array( 'relation' => 'OR', array( 'key' => '_adnvg_hidden_parent', 'compare' => 'NOT EXISTS', ), array( 'key' => '_adnvg_hidden_parent', 'value' => 1, 'compare' => '!=', ), ); $existing_meta_query['relation'] = 'AND'; $query->set( 'meta_query', $existing_meta_query ); } }