I am looking for solution to display related product by multiple product attributes. I've tried "Display Related products for a specific product attribute value in single product pages " StackOverFlow answer code, but it works only for one product attribute value.

How to make the same, but for two product attributes values?

  • Welcome to WordPress Development. I hope you find the answer(s) you are looking for. Our site is different from most - if you have not done so yet, consider checking out the tour and help center to find out how things work. – Matthew Brown aka Lord Matt Sep 16 at 12:05
  • As the code that you tried to use come from StackOverFlow, this question should be asked also in StackOverFlow instead and deleted here. – LoicTheAztec Sep 16 at 15:23

Try this

for($i=0; $i<count($attribute); $i++){
    $attribute[$i] = sanitize_title('pa_' .$attribute[$i]);

$terms = wp_get_post_terms(
'taxonomy' => $attribute
  • Unfortunately, doesn't work. – golansky Sep 16 at 11:36
  • have you tried like this? sending $terms = wp_get_post_terms('taxonomy' => array('color','color2','color3')); check if it works and change the loop as per – Parikshit Parab Sep 16 at 11:43
  • When i'am trying like $terms = wp_get_post_terms('taxonomy' => array('color','color2','color3')); i've got server error 500 - expected ) – golansky Sep 16 at 12:03
  • Hey Parikshit Parab, nice code. Your answer is off to a good start. Please consider explaining how and why this code solves the problem so others can learn from it. Check the guide to answers for further advice. – Matthew Brown aka Lord Matt Sep 16 at 12:06

Well, it's not that simple using the related products functionality itself, but here is my code that selects related products by category + by color (product attribute). I have replaced default related products loop with a [product] shortcode. It's not perfect, but it works.

First of all you need a function that returns term slugs by specified taxonomy and product id. I have just tweaked native WooCommerce function wc_get_product_term_ids

public function my_get_product_term_slugs( $product_id, $taxonomy ) {
    $terms = get_the_terms( $product_id, $taxonomy );
    return ( empty( $terms ) || is_wp_error( $terms ) ) ? array() : wp_list_pluck( $terms, 'slug' );

Then I have used this function inside the main function that returns the product shortcode.

function my_get_custom_related_products( $product, $limit = 4 ) {
    $product_id = $product->get_id();

    $categories = implode( ', ', my_get_product_term_slugs( $product_id, 'product_cat' ) );
    $colors = implode( ', ', my_get_product_term_slugs( $product_id, 'pa_color' ) );

    return do_shortcode( '[products attribute="color" terms="' . $colors .  '" category="' . $categories . '" orderby="rand" limit="' . $limit . '"]' );

And then you call this function (with echo) on anywhere you want. I have replaced the related loop with this function. I also used orderby="rand" inside that [product] shortcode, to get random results after every reload. Of course you can move the the attribute taxonomy name to function parameters to have more control, but I hope this will help you.

I have tried to somehow tweak the native WC related products query, but it was time consuming so I have decided to go with the shortcode approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.