Wordpress get taxonomy name with slug - wordpress

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

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

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.

$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

<?php
$term = get_term_by('slug', $slug, 'category');
$name = $term->name;
$id = $term->term_id;
?>

Related

In WordPress, get all posts having keyword in Title, Tag or Category?

My question is similar to this one, but it's now 7 years old, so I thought I'd ask again.
I'm using the get_posts() function but it seems that whatever is passed as the 's' parameter is only matched against the post title. For example, this code only returns posts containing 'sunflower' in the title, not posts containing 'sunflower' in one of their tags
$args = array( 'numberposts' => 99, 's' => 'sunflower');
$postslist = get_posts( $args );
I'm just starting out with WP development, so maybe I'm overlooking something or using the wrong function... Any pointers will be greatly appreciated!
To additionally get posts with 'sunflower' as a tag you need to add taxonomy parameters like so:
$args = [
'numberposts' => 99,
's' => 'sunflower',
'tax_query' => [
[
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'sunflower'
]
]
];
$postslist = get_posts( $args );
Official docs: https://developer.wordpress.org/reference/functions/get_posts/
IMHO this is much more complicated than it should be, but I managed to get what I want with the following code:
$searchTerm = trim($_REQUEST['search']);
// Get all slugs that are a partial match in tags
$matching_terms_tags = get_terms( array( 'taxonomy' => 'post_tag', 'fields' => 'slugs', 'name__like' => $searchTerm ) );
// Get all slugs that are a partial match in categories
$matching_terms_categories = get_terms( array( 'taxonomy' => 'category', 'fields' => 'slugs', 'name__like' => $searchTerm ) );
// Build taxonomy query
$argsTax = array('numberposts' => 999, 'posts_per_page' => -1, 'nopaging' => true);
$argsTax['tax_query'] = array
(
array
(
'relation' => 'OR',
array ('taxonomy' => 'category', 'field' => 'slug', 'terms' => $matching_terms_categories,'operator' => 'IN',),
array ('taxonomy' => 'post_tag', 'field' => 'slug', 'terms' => $matching_terms_tags, 'operator' => 'IN', ),
),
);
// Get all posts with matching tags and/or matching categories
$postsTax = get_posts($argsTax);
// Also get all posts matching the term, using the regular WP argument 's'
$argsTerms = array('numberposts' => 999, 'posts_per_page' => -1, 'nopaging' => true, 's' => $searchTerm);
$postsSearch = get_posts($argsTerms);
// Merge the 2 result sets and remove duplicates
$postsAll = array_merge($postsSearch, $postsTax);
$postAllNoDupes = array_map("unserialize", array_unique(array_map("serialize", $postsAll)));
foreach ($postAllNoDupes as $post)
{
echo get_the_title($post);
}
wp_reset_query();

Select Custom Post Types Per Category?

Trying to select custom posts by selected category
$args = array('post_type' => 'speakers', 'posts_per_page' => 10,
//'category_name' => 'saturday',// <- solution A
// 'tax_query' => array(
// array(
// 'taxonomy' => 'category',
// 'field' => 'tag_ID',
// 'terms' => array(2),
// ),
// ),//<- solution B
// 'cat' => 2,//<- solution C
'tag__in' => array( 2 ) //<- solution D
// 'tax_query' => array(
// array(
// 'taxonomy' => 'category',
// 'field' => 'tag_ID', //can be set to ID
// 'terms' => '2' //if field is ID you can reference by cat/term number
// )
// )//<- solution E
);
I just add 2 categories for post types but all custom posts are selected not only the posts with category id 2
All 5 solutions didn't work, I also flushed the permalinks by saving and resaving
Try this,
<?php
$args = array('post_type' => 'speakers', 'posts_per_page' => 10,
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy',
'field' => 'slug',
'terms' => 'saturday',
),
),
); ?>
Try to change the custom taxonomy of the post type 'speakers'., as 'category' is the slug name of WP posts.

Take custom category for custom posts

I have a json file with data for custom taxonomy.
And then I need to filter that.. I do not have any problem with postmeta link and other fields. But i can't get the category into json. And then can't filtering that.
I know the answer must be very simple but i can't figure it out for 2 days.
function getItems( $meta_query ) {
$params = array(
'post_type' => 'property',
'nopaging' => true,
'post_status' => 'publish',
'meta_query' => array(),
$meta_query = array('meta_query'=>array(), 'relation' => 'AND');
if (!empty($_POST['city']))
{
$params['meta_query'][] =array(
'key' => 'property_city',
'value' => $_POST['city'],
'compare' => 'LIKE'
);
}
if(!empty($_POST['parish']))
{
$params['meta_query'][] = array(
'key' => 'property_state',
'value' => $_POST['parish'],
'compare' => 'LIKE'
);
}
//..................ETC..........................
What i have for category now:
//if (!empty($_POST['category']))
/*{
$params['meta_query'][] = array(
'taxonomy' => 'pcategory',
'field' => 'slug',
'terms' => $_POST['category']->slug
);
}*/
$itemsQuery = new WP_Query();
$properties = $itemsQuery -> query($params);
// add property details
foreach ($properties as $key => $property) {
// options
$property->optionsDir = get_post_meta($property->ID, 'property', true);
$city = get_post_meta($property->ID, 'property_city', true);
$parish = get_post_meta($property->ID, 'property_state', true);
$link = get_permalink($property->ID);
$category = get_the_terms($property->ID,'pcategory' );
$properties[$key]->link = array(
'link' => $link,
);
//postmeta
$properties[$key]->postmeta = array(
'city' => $city,
'parish' => $parish,
'region' => $region,
'link' => $link,
'category' => $category,
);
}
return $properties;
}
$category = get_the_terms($property->ID,'pcategory' );
Above code will return, all details like, term id, name, slug etc. information about that term, as an array object.
You need to extract the required details.
Also, "pcategory" is custom taxonomy.
Therefore, in below code,
$properties[$key]->postmeta = array(
'city' => $city,
'parish' => $parish,
'region' => $region,
'link' => $link,
'category' => $category,
);
'category' will not be interpreted properly, as it is custom taxonomy and not default wordpress taxonomy.
If these parameters are sent to WP_Query(), you can mention 'tax_query' as below,
'tax_query' => array(
array(
'taxonomy' => 'pcategory',
'field' => 'slug',
'terms' => 'demo'
)
Here, "demo" is example slug for custom taxonomy.
Even, single post can have multiple category associated with it. Therefore, you need to extract desired category details.
In 'tax_query' you can also specify, "term_id" or other details.

Wordpress QP_Query operator

I trying to create a query to get some news from category A,B,C
I wont a article which is in all categories.
Array
(
[post_type] => catalog
[order] => DESC
[orderby] => date
[status] => publish
[paged] => 1
[tax_query] => Array
(
[0] => Array
(
[taxonomy] => catalog_category
[field] => id
[include_children] => 1
[operator] => AND
[terms] => Array
(
[0] => 12
[1] => 17
[2] => 43
)
)
)
)
If I use "operator" "IN" I get all articles if I use "AND" I get none results.
List of my cats and articles:
As you can see, I have only 1 article in all 3 categories.
What is my mistake?
Your query seems fine but not sure what is wrong, anyways, as an alternative you may give it a try to get all posts (custom post type = catalog) which belongs to all three categories in (a, b and c), not less/more but exact match of all categories.
$cat_Ids = array();
// get id from each category name
foreach(array('a', 'b', 'c') as $cat) {
$cat_Ids[] = get_cat_ID($cat)
}
$args = array(
'post_type' => 'catalog', // <-- post type is 'catalog', not 'news', right ?
'category__and' => $cat_Ids,
'orderby' => 'date',
'order' => 'DESC',
'status' => 'publish'
);
$query = new WP_Query($args);

How to list categories and subcategories in WordPress

How to display all CATEGORIES and SUB-CATEGORIES in wordpress if a category having 0 posts also. I tried but it is displaying categories which are having at least 1 post in a category . I want to display categories which are having 0 posts also.
Thank You
See the get_categories function, there is a parameter called "hide_empty". For instance:
<?php $cats = get_categories('hide_empty=0'); ?>
use get_categories() function to get all categories.
$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1, // hide empty categories [0] false
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'category',
'pad_counts' => false
);
$categories = get_categories( $args );
after you have stored all categories in a variable use print_r to see all the values of the array that you can use.
echo "<pre>";
print_r( $slider_category );
echo "</pre>";
when you use print_r you will see like this
Array (
[0] => stdClass Object
(
[term_id] => 11
[name] => Architecture
[slug] => architecture
[term_group] => 0
[term_taxonomy_id] => 11
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 3
[cat_ID] => 11
[category_count] => 3
[category_description] =>
[cat_name] => Architecture
[category_nicename] => architecture
[category_parent] => 0
))
play with the code and you will get what you want.

Resources