admin管理员组文章数量:1300359
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.
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.
Share Improve this question edited Mar 22, 2021 at 13:08 Zeth asked Mar 22, 2021 at 12:59 ZethZeth 9282 gold badges13 silver badges43 bronze badges1 Answer
Reset to default 6You'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.
本文标签: How to get all term meta for a taxonomygetting termmeta for taxonomy
版权声明:本文标题:How to get all term meta for a taxonomy - getting term_meta for taxonomy 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741662623a2391130.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论