Billy-3k Posted January 27, 2020 Share Posted January 27, 2020 Hello, I need to hide products with same title from WooCommerce shop page and i'm trying to achieve that with a custom loop. For example i have 5 products called "Plex" and the only diferrence between them is the SKU field and the color. I don't care which of the same product will be displayed in the shop page, i just want to display only one of all these products. I created a loop which is working and does hide products with same name but i have a problem. If i set posts_per_page=8 it shows less because it counts the hidden products also. $args = array( 'post_type' => 'product', 'posts_per_page' => 8 ); $query = new WP_Query($args); $list = array(); while ($query->have_posts()) : $query->the_post(); if(in_array(get_the_title(), $list)){ continue; } $list[] = get_the_title(); ?> <li><?php wc_get_template_part( 'content', 'product' ); ?></li> <?php endwhile; wp_reset_postdata(); P.S. I don't want to remove these products, i just want to hide them from shop loop! Any ideas what is the problem with the loop? Is there a better way to achieve that? Quote Link to comment https://forums.phpfreaks.com/topic/309943-product-loop-excluding-values-with-same-name/ Share on other sites More sharing options...
Barand Posted January 27, 2020 Share Posted January 27, 2020 When you process the query and store in an array, make the array index the product name $list['Plesk'] = [ product data ] Since array keys must be unique you will end up with only one. Quote Link to comment https://forums.phpfreaks.com/topic/309943-product-loop-excluding-values-with-same-name/#findComment-1573844 Share on other sites More sharing options...
maxxd Posted January 27, 2020 Share Posted January 27, 2020 10 minutes ago, Billy-3k said: Is there a better way to achieve that? Are you using product variations? If I'm remembering correctly what you're trying to achieve is exactly what that functionality is for. Quote Link to comment https://forums.phpfreaks.com/topic/309943-product-loop-excluding-values-with-same-name/#findComment-1573846 Share on other sites More sharing options...
Billy-3k Posted January 27, 2020 Author Share Posted January 27, 2020 12 minutes ago, Barand said: When you process the query and store in an array, make the array index the product name $list['Plesk'] = [ product data ] Since array keys must be unique you will end up with only one. I'm not sure what you mean with $list['Plesk'] = [ product data ]. You think it will help with the pagination issue? Quote Link to comment https://forums.phpfreaks.com/topic/309943-product-loop-excluding-values-with-same-name/#findComment-1573847 Share on other sites More sharing options...
Billy-3k Posted January 27, 2020 Author Share Posted January 27, 2020 16 minutes ago, maxxd said: Are you using product variations? If I'm remembering correctly what you're trying to achieve is exactly what that functionality is for. Yeap, i'm using variations but it's more complex than that, there's a reason i can't use variation for this problem. I must find a way to exclude same name products(or same fields) value from the shop page. Quote Link to comment https://forums.phpfreaks.com/topic/309943-product-loop-excluding-values-with-same-name/#findComment-1573848 Share on other sites More sharing options...
Billy-3k Posted March 25, 2020 Author Share Posted March 25, 2020 Ok so i've found the solution by using a function, in case anyone in the future needs it. This fuction hides same title products in shop page, in category and in search results. 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; } Quote Link to comment https://forums.phpfreaks.com/topic/309943-product-loop-excluding-values-with-same-name/#findComment-1575786 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.