I'm trying to figure out the php syntax here, so I can show only related products that are of the current taxonomy term. I would like the $args filter 'product_brand' to dynamically grab the current products term.

So I need a method of getting the slug for the current product term. I've tried this but it's not working, and is returning more than one term.

$mjrelatedproducts = get_brands( $post->ID )->slug;

$args = apply_filters('woocommerce_related_products_args', array(
    'post_type'             => 'product',
    'ignore_sticky_posts'   => 1,
    'product_brand'         => $mjrelatedproducts,
    'no_found_rows'         => 1,
    'posts_per_page'        => $posts_per_page,
    'orderby'               => $orderby,
    'post__in'              => $related,
    'post__not_in'          => array($product->id)
) );
  • But what if the current product/post has multiple brands? – Tom J Nowell Aug 9 '13 at 16:35
  • The Products are set to only ever have one brand – KingLouie Aug 9 '13 at 17:16
  • does the API written for everybody not just you know that though? If I make sure my blog posts are only ever in 1 tag at a time, that doesn't magically change all the APIs, I still get arrays back even if they only have 1 thing in them – Tom J Nowell Aug 9 '13 at 21:01
  • hmm, I'm not sure. I do seem to be getting multiple results, which I didn't think would happen. I assumed I would get no results at all if its was wrong. – KingLouie Aug 9 '13 at 21:35

I prefer to use get_the_ID() for things like that, which will return the ID of the current page/post.

$mjrelatedproducts = get_brands( get_the_ID() )->slug;

That should return the slug of the current page/post.

| improve this answer | |

What about just using get_the_terms()?

<?php get_the_terms( $id, $taxonomy ); ?> 
| improve this answer | |
  • How would i apply that to the args array? For example, the term slug on the page I'm testing it on is 'aat' this is the return i would need. – KingLouie Aug 9 '13 at 19:06

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.