Skip to content
Advertisement

WooCommerce – Hide products with same title

I need to hide products with same title from shop page. I have many products with different SKU but same name.

Is it possible to achieve that with a distinct function like this or should i create a custom loop?

add_filter( 'posts_distinct', function ( $distinct ) {
    //if same name update_post_meta( $product_id, '_visibility', '_visibility_search' );
return 'DISTINCT'; });

Advertisement

Answer

Ok so i’ve found the solution by using a function, in case anyone in the future needs it.

add_filter( 'posts_groupby', 'custom_posts_groupby', 10, 2 );
function custom_posts_groupby( $groupby, $query ) {
     global $wpdb;

     if ( is_main_query() && (is_shop() || is_product_category() || is_search() )) {
         $groupby = "{$wpdb->posts}.post_title";
     }

     return $groupby;
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement