This will not be the fastest way to achieve it, – but it will do the trick. If you have a bazillion posts (I’d say more than 1000) – or if you’re server is slow, then this might be too heavy. But I wouldn’t hesitate to use it on any of my sites.
If you want something fast, then you should probably go with some SQL of some sort.
<?php
$episode_query = new WP_Query([
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 999,
'order' => 'DESC',
'orderby' => 'date'
]);
$terms = [];
if( $episode_query->have_posts() ):
while( $episode_query->have_posts() ):
$episode_query->the_post();
$post = get_post();
// Tags
$tags = get_the_terms( $post, 'post_tag' );
if( !empty( $tags ) ):
foreach( $tags as $tag ):
if( does_array_contain_term( $tag, $terms ) ){
$terms[] = $tag;
}
endforeach; // foreach( $tags as $item ):
endif; // if ( !empty( $tags ) )
// Category
$categories = get_the_terms( $post, 'category' );
if( !empty( $categories ) ):
foreach( $categories as $cat ):
if( does_array_contain_term( $cat, $terms ) ){
$terms[] = $cat;
}
endforeach; // foreach( $categories as $item ):
endif; // if ( !empty( $categories ) )
endwhile; // while( $episode_query->have_posts() ):
wp_reset_query();
endif; // if( $episode_query->have_posts() ):
function does_array_contain_term( $term_to_check, $terms ){
$add_term = true;
foreach( $terms as $term ){
if( $term->slug == $term_to_check->slug ){
$add_term = false;
}
}
return $add_term;
}
// TO SEE THE RESULT
echo '<pre>';
print_r($terms);
echo '</pre>';
?>