TL;DR: I’m trying to get the meta_value display_type for a taxonomy, but it returns blank (even though I can see it in the database).
This question wants to access a term meta that is on a the taxonomy: product_cat, which is a taxonomy made by WooCommerce.
I’m asking this here instead of StackOverflow, since it’s more related to WordPress than to WooCommerce.
In my database, I can see that there are a couple of lines in the database for the term with the $term->ID = 15.
meta_id | term_id | meta_key | meta-value --------------------------------------------------- 1 | 15 | display_type | subcategories 2 | 15 | thumbnail_id | 0 204 | 15 | product_.... | 45 676 | 15 | an_acf_field | test 676 | 15 | _an_acf_field | field_123abc123abc
But for some reason then this returns empty:
$term = get_queried_object(); // in the file taxonomy-product-cat.php $display_type = get_term_meta( $term->ID, 'display_type' ); // $term->ID = 15 echo '<pre>'; print_r($display_type); echo '</pre>'; // Returns empty
So I figured that I could debug it, by getting all term_meta for that ID (15). But I can’t find any WordPress-function that get’s all the meta_fields?
Solution attempts
Attempt 1 – Using WPDB
I could do something like this:
global $wpdb; $test = $wpdb->get_results( "SELECT * FROM wp_termmeta WHERE term_id = '15'");
But it just seems like a non-WordPress-kinda-way.
Attempt 2 – get_term_meta without key
A wild guess was to just do this:
$term = get_queried_object(); $display_type = get_term_meta( $term->ID ); // !! No key defined echo '<pre>'; print_r($display_type); echo '</pre>'; // Still returns empty
Attempt 3 – Looking into WooCommerce documentation
I can see that WooCommerce access the display_type itself here
like this:
$display_type = get_term_meta( $item->term_id, 'display_type', true );
… So it baffles me that it doesn’t work for me.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You’re using $term->ID. Taxonomy terms do not have an ID property. They have a term_id property:
$display_type = get_term_meta( $term->term_id );
You’ll notice that WooCommerce is using this, while your attempts are not.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0