If I understood you correctly, you are looking for get_the_terms()
, which..
Retrieves the terms of the taxonomy that are attached to the post.
You could for example have helper functions like these,
// functions.php
function my_sports_post_terms( int $post_id ) : array {
$taxonomies = ['basketball', 'volleyball', 'baseball'];
$terms = [];
foreach ($taxonomies as $taxonomy) {
$terms[$taxonomy] = my_sports_post_tax_terms($post_id, $taxonomy);
}
return $terms;
}
// Force array return value as get_the_terms() may not always return an array
function my_sports_post_tax_terms( int $post_id, string $taxonomy ) : array {
$terms = get_the_terms( $post_id, $taxonomy );
return $terms && is_array($terms) ? $terms : [];
}
And then use the first one in the Loop like so.
// some template file
while ($query->have_posts()) {
$query->the_post();
$terms = my_sports_post_terms( get_the_ID() );
// code..
}