How do I access a single term from a post?

It’s a little bit hard to be sure, what are you asking exactly, but… Let me try to answer…

So somewhere in single.php you’re getting terms for current post using this code:

$terms = wp_get_post_terms($post->ID, 'mytax', array("fields" => "all"));

and you want to get the ID of first term from that list?

If so, you can achieve this with this code:

$terms = wp_get_post_terms($post->ID, 'mytax', array("fields" => "all"));
$term_id = false;
if ( $terms ) {
    $term_id = $terms[0]->term_id;
}

Leave a Comment