WordPress.org

Ready to get started?Download WordPress

Forums

Equivalent to in_category() for custom taxonomy (8 posts)

  1. enailor
    Member
    Posted 3 years ago #

    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?

  2. dermotholmes
    Member
    Posted 2 years ago #

    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

  3. jvandelaar
    Member
    Posted 2 years ago #

    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

  4. dermotholmes
    Member
    Posted 2 years ago #

    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.

  5. jvandelaar
    Member
    Posted 2 years ago #

    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!

  6. Looks like has_term() may be what you wanted.

  7. dermotholmes
    Member
    Posted 2 years ago #

    ...there's always an easier way haha

  8. 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!

Topic Closed

This topic has been closed to new replies.

About this Topic