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.
Related
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".
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);
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
I have this output:, and I want to get to "Center".
[field_affiliation] => Array(
[und] => Array(
[0] => Array(
[tid] => 30
[taxonomy_term] => stdClass Object(
[tid] => 30
[vid] => 2
[name] => Center
Can I use this method?
$affiliation=$node->field_affiliation['und']['0']['value']['taxonomy_term']['name'];
or how can I use:
drupal_array_get_nested_value(); - I cannot understand the documentation
Try this it will work
as the name is under the object 'taxonomy_term'
$affiliation=$node->field_affiliation['und']['0']['taxonomy_term']->name;
When adding custom php code for a conditional action, drupal/ubercart provides two php variables ($order and $account) that hold information about the order and account for the checkout that was just completed.
I can't find anywhere on the internet documentation about how these objects are structured...anyone know where this documentation is or how these objects are setup???
thanks
Here's what the $order object looks like on a fairly standard install (might be a bit different depending on your installation. Of course the products in the order dictate what the items section looks like:
stdClass Object
(
[order_id] => 123
[uid] => 456
[order_status] => payment_received
[order_total] => 100
[product_count] => 1
[primary_email] => test#example.com
[delivery_first_name] => Test
[delivery_last_name] => Customer
[delivery_phone] => 123-123-1234
[delivery_company] => ABC Company, Inc.
[delivery_street1] => 123 Easy St.
[delivery_street2] =>
[delivery_city] => Anytown
[delivery_zone] => 39
[delivery_postal_code] => 12345
[delivery_country] => 840
[billing_first_name] => Test
[billing_last_name] => Customer
[billing_phone] => 123-123-1234
[billing_company] => ABC Company, Inc.
[billing_street1] => 123 Easy St.
[billing_street2] =>
[billing_city] => Anytown
[billing_zone] => 39
[billing_postal_code] => 12345
[billing_country] => 840
[payment_method] => credit
[data] => Array
(
[cc_data] => ***encrypted credit card data***
)
[created] => 1295455508
[modified] => 1295457962
[host] => 127.0.0.1
[products] => Array
(
[0] => stdClass Object
(
[order_product_id] => 245
[order_id] => 123
[nid] => 5
[title] => Test Product
[manufacturer] =>
[model] => TEST-PRODUCT-SKU
[qty] => 1
[cost] => 100.00000
[price] => 100.00000
[weight] => 0
[data] => Array
(
[attributes] => Array
(
)
[shippable] => 1
[module] => uc_product
)
[order_uid] => 456
)
)
[payment_details] =>
[quote] => Array
(
[method] => flatrate_1
[accessorials] => 0
[rate] => 7.00000
[quote_form] =>
)
[line_items] => Array
(
[0] => Array
(
[line_item_id] => subtotal
[type] => subtotal
[title] => Subtotal
[amount] => 100
[weight] => 0
[data] =>
)
[1] => Array
(
[line_item_id] => 194
[type] => shipping
[title] => Flat Rate Shipping
[amount] => 7.00000
[weight] => 1
[data] =>
)
)
)
$account is the user object.
$order is the ubercart order object.
There are some minimum defined values for both of these objects, but they can contain anything really. The reason is, that Drupal will allow modules to expand the user object, while ubercart will allow modules to expand the order object.
The best thing to do in such situations is to inspect the objects to figure out how to get to what you need.
The devel module will allow you to pretty print variables using dsm() or dump the variable t a log file using dd(). Those are two ways to get to the variable info.