Jump to content

Recommended Posts

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?

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?

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.

  • 1 month later...

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;
}

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.