"Trying to get property 'taxonomy' of non-object" when using get_terms() - wordpress

I got the list of the terms of a specific taxonomy (GENRES), using this code:
$categories = get_terms('genre', 'orderby=name&hide_empty=0');
if($categories){
echo '<select>';
foreach ($categories as $cat){
echo "<option value='{$cat->term_id}'>{$cat->name}</option>";
}
echo '</select>';
}
The code works, but I want to get also the title of the taxonomy (GENRES), so for this I used print_r($categories); and I got this output:
Array (
[0] => WP_Term Object (
[term_id] => 33
[name] => Action
[slug] => action
[term_group] => 0
[term_taxonomy_id] => 33
[taxonomy] => genre
[description] =>
[parent] => 0
[count] => 1
[filter] => raw
)
[1] => WP_Term Object (
[term_id] => 34
[name] => Comedy
[slug] => comedy
[term_group] => 0
[term_taxonomy_id] => 34
[taxonomy] => genre
[description] =>
[parent] => 0
[count] => 2
[filter] => raw
)
)
Now for getting the title I used print_r($categories->taxonomy);, but I got "Trying to get property 'taxonomy' of non-object". Why does this error occur, and how to get the title of taxonomy using my implementation?

Function get_terms() returns an array of objects, not single object.
So, just use $categories[0]->taxonomy and you will get an output "genre".

Related

Get_parent_name of custom post type

I Use "get_queried_object" and get an array:
stdClass Object ( [term_id] => 10 [name] => Kem nền [slug] => kem-nen [term_group] => 0 [term_taxonomy_id] => 10 [taxonomy] => muc-san-pham [description] => [parent] => 16 [count] => 6 [filter] => raw )
And I know [Parent]= 16. I want to get [Name] of [Parent] = 16 . But i don't know how to get name? Please help me!
Based on your query you will get the parent id.
So you can use this to get parent name.
get_the_title(16);

get_the_terms does not return cat_id

i am usign this code to get the cat_id
$terms = get_the_terms( $id,'category' );
foreach( $terms as $term ){
$cats[] = $term->cat_ID;
}
However when i do
print_r($terms);
This is what i get
Array
(
[10] => stdClass Object
(
[term_id] => 10
[name] => Technology
[slug] => technology
[term_group] => 0
[term_taxonomy_id] => 10
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 3
[object_id] => 1
[filter] => raw
)
)
As you can see the cat_ID is not included and there is [filter] => raw. What is the [filter] => raw ?
Note: I am using wordpress 4.0
Thank you,
MMK.
term_id is same as cat_ID. filter contains the context under which the contents of the post have been filtered.
raw => unsanitized data
Read here about filter

Wordpress get taxonomy name with slug

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;
?>

WordPress get_terms returning empty array

I'm having an issue with the get_terms() function.
If I use:
<?php
$terms = get_terms( 'location' );
print_r($terms);
?>
I get an array back:
Array ( [0] => stdClass Object ( [term_id] => 8 [name] => Aberdare [slug] => aberdare [term_group] => 0 [term_taxonomy_id] => 8 [taxonomy] => location [description] => [parent] => 7 [count] => 1 ) [1] => stdClass Object ( [term_id] => 10 [name] => Aberdeen [slug] => aberdeen [term_group] => 0 [term_taxonomy_id] => 10 [taxonomy] => location [description] => [parent] => 9 [count] => 14 ) [2] => stdClass Object ( [term_id] => 17 [name] => Aberdeenshire [slug] => aberdeenshire [term_group] => 0 [term_taxonomy_id] => 17 [taxonomy] => location [description] => [parent] => 9 [count] => 41 ) [3] => stdClass Object ( [term_id] => 1203 [name] => Aberdour [slug] => aberdour [term_group] => 0 [term_taxonomy_id] => 1203 [taxonomy] => location [description] => [parent] => 446 [count] => 1 );
But if I try and get the terms with the parent of of any number of child_of the array comes back empty.
<?php
$terms = get_terms( 'location', array('parent' => 9) );
print_r($terms);
?>
I get this:
Array ( )
Does any one know what i'm doing wrong?
Thanks
Pete
Ok I solved the issue.
Was a bit strange really. All my terms were added via a script using the wp_insert_term function. When I went to the edit-tags.php?taxonomy=location page in the dashboard could only see the top three terms in the list on the right, but I could see the others in he term cloud on the left.
I then went into one of the terms and saved it and they all appeared in the list on the right hand side and also in my array.
I'm guessing their must of been some sort of caching on the terms list and it needed me to save one through the dashboard in order to see the hierarchy.
A strange one I must admit.
Pete
The hierarchy is cached, and is not invalidated automatically after wp_insert_term. You can call
delete_option("{$taxonomy}_children");
manually to force a regeneration on next access to the hierarchy.

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