WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)
) );
share|improve this question

migrated from stackoverflow.com Aug 9 '13 at 16:02

This question came from our site for professional and enthusiast programmers.

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 – Mike Jackson 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. – Mike Jackson 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.

share|improve this answer

What about just using get_the_terms()?

<?php get_the_terms( $id, $taxonomy ); ?> 
share|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. – Mike Jackson Aug 9 '13 at 19:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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