Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save shameemreza/bf951d485c50de88d955c455d8dda1b3 to your computer and use it in GitHub Desktop.

Select an option

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.
/**
* 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;
}
@shameemreza
Copy link
Author

CleanShot 2025-11-17 at 10 42 13

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