• Hello fellow WP users and developers.

    I am having a tough time with something and hoping someone has an answer. I need an equivalent to the WP conditional in_category().

    I have a query that is pulling from a custom taxonomy. Using the more general examples, let’s say that taxonomy is Movies. The terms would be “Action”, “Drama” and “Comedy”

    I need something that will check to see if the post is within the term “Drama”.

    As I understand Categories, “Category” is a taxonomy and the specific defined categories are “terms” within that category. Using the in_category() allows you to see if the post is within one of the defined terms of the taxonomy “Category”.

    I need something that will do the same for my custom taxonomies. Anyone have a solution for this?

Viewing 7 replies - 1 through 7 (of 7 total)
  • I’m sure there is a more direct way to go about this, however this is what I ended up with for a similar problem.

    Assuming your in the loop of the current post you want to check against:

    $termsObjects = wp_get_object_terms($post->ID, 'Movies');
    
    //Assuming your post only has one category, if theres more it will return multiple objects in it's return array:
    
    $currentCustomCat = $termsObjects[0]->slug;
    //you can have 'name' instead of 'slug' if that helps too 
    
    if( $currentCustomCat == "Drama"){
       //It matched, do stuff here
    }

    Let me know if you need any explanation of above,
    D

    Awesome piece of script dermotholmes! Worked a treat for my application with woocommerce.

    However, I’d like to target all the posts in a parent category. Eg, top category is ‘action’, then child categories could be ‘Action Comedies’, ‘Sci-fi Action’, ‘Martial Arts’ etc..

    So all posts from all of the categories under ‘action’ will be affected. Hope my request makes sense

    Hey jvandelaar, have a read over the WordPress function wp_get_object_terms(), you should be able to do what your after like so:

    if( count(wp_get_object_terms( $post->ID, 'action')) > 0 ){
       //this post has at least one term in the action taxonomy associated with it, so do stuff...
    }

    Whats going on there?

    wp_get_object_terms( $post->ID, ‘action’)

    returns an array containing any and all terms that this post has under the ‘actions’ taxonomy. If the post has no terms under the action category you get back an empty array. So, by wrapping this with count() as in…

    count(wp_get_object_terms( $post->ID, ‘action’))

    you get a the number of terms this post has under the ‘action’ taxonomy.

    as long as thats more then zero…

    > 0

    you know this post has at least one term from the ‘action’ taxonomy on it so you can now do what ever you want in that if block.

    Peace out cub scout.

    Thanks heaps mate! However it’s almost there..

    <?php if( count(wp_get_object_terms( $post->ID, 'hire')) > 0 ) echo 'Hire!'; else echo 'Buy!'; ?>

    That’s the line I’m using. The taxonomy these terms are using is “product_cat”, and there are no posts/products directly under the “hire” category. Only those under child categories.

    It changes the echo from ‘hire’ or ‘buy’ if I change the 0 to 1 so it’s definitely taking effect.. just not effecting only those in a category under ‘hire’. Will post what I find!

    Looks like has_term() may be what you wanted.

    …there’s always an easier way haha

    Yeah, there often is ?? — of course, I only found it because I figured that there just had to be something I wasn’t remembering at first. And because I’m one of those freaks who digs around in core for fun reading!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Equivalent to in_category() for custom taxonomy’ is closed to new replies.