get_terms() for custom taxonomy related to another taxonomy

I have this weird thing to do and I have no idea how to do it. Ok, so:

I have two custom taxonomies: collections and categories (both are prefixed, but for the sake of simplicity, I’ll use these short names). A post will have a category and will be added into a collection.

Read More

What I have to do is to display all posts from a specific taxonomy – categories (already did this) and only show collections terms that have posts in the selected category. Some kind of hide_empty but based on another taxonomy. Make sense?

Since I use WPML plugin (that is known for adding stuff into sql query), I would like a solution that not involve the rewrite of the whole query.

So, any idea would be welcomed. Thanks!

Related posts

Leave a Reply

2 comments

  1. You can try getting all the posts IDs from the first taxonomy

    $objects = get_posts( array( 'category' => 'history', 'numberposts' => -1, ) );
    foreach ($objects as $object) {
        $objects_ids[] = $object->ID;
    }
    

    Then get the terms from the second taxonomy associated with them:

    $collections = wp_get_object_terms( $object_ids, 'collections' );