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 – Pieter Goosen Dec 12 '14 at 10:54

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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