Is it possible, within the loop (or given the post ID) to retrieve the name/slug of the main custom taxonomy of a custom post?

What I really need is the taxonomy term, and that one I can find it with:

wp_get_post_terms($post->ID, 'taxonomy-name);

but in order to do it I need to provide the taxonomy name. What if I don't know it in advance?

Any idea?

  • If you need to get and display the post terms without knowing the taxonomy, look at this post I have recently done Dec 12, 2014 at 10:54

1 Answer 1


I have found an hopefully elegant solution

$taxonomy_names = get_object_taxonomies( $post, 'objects' );
foreach ($taxonomy_names as $tax) {
    if ($tax->query_var != 'tag') { 
    // needed because $taxonomy names containes more than one query_var
        $current_tax_name_slug = $tax->query_var;

This one works for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.