10

How can I get a taxonomy id or name with only the taxonomy slug ?

I guess I'm looking for the equivalent of get_term_by() but for taxonomies.

Edit : I must specify that I'm trying to get the tax ID of a WooCommerce product attribute.

Thanks

4 Answers 4

18

WordPress does provide a function to get the taxonomy information from its slug.

$taxonomy_details = get_taxonomy( $slug );

This will return the taxonomy details as an object, which includes the various labels for the taxonomy. For example here's the returned object when called for the standard Category taxonomy, e.g. get_taxonomy( 'category' );

stdClass Object
(
    [labels] => stdClass Object
        (
            [name] => Categories
            [singular_name] => Category
            [search_items] => Search Categories
            [popular_items] => 
            [all_items] => All Categories
            [parent_item] => Parent Category
            [parent_item_colon] => Parent Category:
            [edit_item] => Edit Category
            [view_item] => View Category
            [update_item] => Update Category
            [add_new_item] => Add New Category
            [new_item_name] => New Category Name
            [separate_items_with_commas] => 
            [add_or_remove_items] => 
            [choose_from_most_used] => 
            [not_found] => No categories found.
            [menu_name] => Categories
            [name_admin_bar] => category
        )

    [description] => 
    [public] => 1
    [hierarchical] => 1
    [show_ui] => 1
    [show_in_menu] => 1
    [show_in_nav_menus] => 1
    [show_tagcloud] => 1
    [show_in_quick_edit] => 1
    [show_admin_column] => 1
    [meta_box_cb] => post_categories_meta_box
    [rewrite] => Array
        (
            [hierarchical] => 1
            [slug] => category
            [with_front] => 1
            [ep_mask] => 512
        )

    [query_var] => category_name
    [update_count_callback] => 
    [_builtin] => 1
    [cap] => stdClass Object
        (
            [manage_terms] => manage_categories
            [edit_terms] => manage_categories
            [delete_terms] => manage_categories
            [assign_terms] => edit_posts
        )

    [name] => category
    [object_type] => Array
        (
            [0] => post
        )

    [label] => Categories
)

Source: https://codex.wordpress.org/Function_Reference/get_taxonomy

2
  • 1
    The slug is not the same as the name of the taxonomy. Your example only works because they happen to be equal in this particular case. get_taxonomy takes a taxonomy name, not a slug.
    – Hjalmar
    May 15, 2015 at 11:26
  • @Hjalmar I think the original poster was referring to taxonomy->name as the slug, and taxonomy->label as the name. I'm facing the same issue as him with WC attributes.
    – David
    Oct 3, 2015 at 11:50
3

As the accepted answer does not answer the question, I provide an answer here even though the question is very old.

The third (required) argument to get_term_by() is the name of the taxonomy itself, and so this function can not be used.

get_taxonomies() can't be used either because then you would have to match the entire rewrite array, which you probably don't have access to.

So the only way I found was to use the private array $wp_taxonomies:

function get_tax_name_from_slug($slug){
  foreach ($wp_taxonomies as $key => $value) {
    if ($value->rewrite['slug'] === $slug){
        return $key;
    }
  }
}

I really hope Wordpress will provide a way to do this without accessing their internal data structures.

0
$args = array(
                    'post_type' => 'awards',
                    'post_status' => 'publish',
                    'posts_per_page' => 4,
                     'orderby' => 'ID',
                     'order' => 'DESC',
                    'tax_query' => array(
                        'relation' => 'AND',
                        array(
                            'taxonomy' => 'awards_categories',
                            'field' => 'slug',
                            'terms' => $award_solution
                        ),
                        array(
                            'taxonomy' => 'year',
                            'field' => 'slug',
                            'terms' => $yearvalue
                        ),
                    )
                );

how we fetch this with wp select query

-4
<?php 
    $term = get_term_by('slug', $slug, 'category'); 
    $name = $term->name; 
    $id = $term->term_id;
?>
1
  • 12
    This does not answer the question. It will only find the name of a term, not a taxonomy.
    – David
    Oct 3, 2015 at 11:42

Your Answer

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

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