Get primary term from Yoast SEO

/**
 * Get term
 *
 * @param string $taxonomy Taxonomy name.
 * @param string $field    Field name.
 * @param int    $post_id  ID of the post.
 *
 * @return string/array Term Get singular term from taxonomy
 */
function prefix_get_term( $taxonomy = '', $post_id = '' ) {
	if ( empty( $post_id ) ) {
		$post_id = get_the_ID();
	}

	$terms = array();

	// Yoast SEO support
	if ( function_exists( 'yoast_get_primary_term_id' ) ) {
		$term_id = yoast_get_primary_term_id( $taxonomy, $post_id );

		if ( $term_id ) {
			$terms[] = get_term( $term_id, $taxonomy );
		}
	}

	if ( ! ( $terms ) ) {
		$terms = get_the_terms( $post_id, $taxonomy );
	}

	if ( ! ( $terms ) ) {
		return;
	}

	return $terms[0];
}