How to get all term meta for a taxonomy – getting term_meta for taxonomy
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.
Leave an answer