Created
November 17, 2025 04:43
-
-
Save shameemreza/bf951d485c50de88d955c455d8dda1b3 to your computer and use it in GitHub Desktop.
Custom code to hide variations for selected products when using the Variations as Single Products plugin.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Hide variations for specific products when using Variations as Single Products plugin | |
| * Add product slugs to the $target_products array below | |
| */ | |
| add_filter( 'posts_clauses', 'vaspfw_hide_specific_variations', 99998, 2 ); | |
| function vaspfw_hide_specific_variations( $clauses, $query ) { | |
| global $wpdb; | |
| // Only apply to variation queries | |
| if ( ! isset( $query->query_vars['vaspfw_single_variation_filter'] ) || | |
| 'yes' !== $query->query_vars['vaspfw_single_variation_filter'] ) { | |
| return $clauses; | |
| } | |
| // UPDATE THESE: Add your product slugs here | |
| $target_products = array( | |
| 'variable-product-1', | |
| 'simply-solid-necktie', | |
| 'simply-solid-vest', | |
| ); | |
| // Get product IDs | |
| $product_ids = array(); | |
| foreach ( $target_products as $slug ) { | |
| $product = get_page_by_path( $slug, OBJECT, 'product' ); | |
| if ( $product ) { | |
| $product_ids[] = absint( $product->ID ); | |
| } | |
| } | |
| if ( ! empty( $product_ids ) ) { | |
| $ids_safe = implode( ',', $product_ids ); | |
| // Hide variations but keep parent products visible | |
| $clauses['where'] .= " AND {$wpdb->posts}.ID NOT IN ( | |
| SELECT ID FROM {$wpdb->posts} | |
| WHERE post_type = 'product_variation' | |
| AND post_parent IN ({$ids_safe}) | |
| )"; | |
| // Ensure parent products remain visible | |
| $clauses['where'] = str_replace( | |
| 'AND 0 = (select count(*) as variationcount', | |
| "AND ({$wpdb->posts}.ID IN ({$ids_safe}) OR 0 = (select count(*) as variationcount", | |
| $clauses['where'] | |
| ); | |
| $clauses['where'] = preg_replace( | |
| "/(from.*?vaspfw_products\.post_status = 'publish'\))/", | |
| '$1)', | |
| $clauses['where'], | |
| 1 | |
| ); | |
| } | |
| return $clauses; | |
| } |
Author
shameemreza
commented
Nov 17, 2025

Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment