Wordpress gets the name of the taxonomy with bullets

How can I get a taxonomy identifier or name using only the taxonomy pool?

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

Edit: I must indicate that I am trying to get the tax id for the WooCommerce product attribute.

thank

+5
source share
4 answers
<?php 
    $term = get_term_by('slug', $slug, 'category'); 
    $name = $term->name; 
    $id = $term->term_id;
?>
+1
source

WordPress provides a function to get taxonomy information from its bullet.

$taxonomy_details = get_taxonomy( $slug );

This will return the details of the taxonomy as an object that includes various labels for the taxonomy. For example, here is the returned object when called for a standard category taxonomy, for example.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
)

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

+7

, , .

The third (required) argument get_term_by()is the name of the taxonomy itself, and therefore this function cannot be used.

get_taxonomies() cannot be used either because then you have to map the entire rewrite array, which you probably do not have access to.

So the only way I found was to use a 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 that Wordpress will provide you with a way to do this without access to their internal data structures.

+2
source
$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 do we get this with a wp select query

0
source

All Articles