This will return an array of term objects, if you need these processed to be hierarchical or something, that’s pretty easy, I think the challenge you were having was getting the term objects.
//get posts
$args = array(
//basic stuff
'post_status' => 'publish',
//meta query
'meta_query' => array(
array(
'key' => 'your_key',
'value' => '1'
)
)
);
$posts = new WP_Query( $args );
//get categories from posts and amalgamate them
$categories = array();
foreach( $posts as $post ) {
$new_cats = wp_get_object_terms( $post->ID, 'your-taxonomy-slug' );
$categories = array_merge(
$categories,
array_diff(
$categories,
$new_cats
)
);
}
I feel like there might be something that you could do with the get_terms
hook, but I can’t put my finger on it (shy of querying for every term to see if it has posts that meet the condition, but that’s way less efficient than my method I think).