admin管理员组文章数量:1122832
Im trying to build a taxonomy query to only display the terms that have a certain custom field value assigned to it. The custom field holds a boolean value, so I tried:
$args = array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'meta_key' => 'featured',
'meta_value' => true
);
Also:
$args = array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'meta_query' => array(
array(
'key' => 'featured',
'value' => true,
'compare' => '='
)
)
);
None of these returned the expected results. What am I missing?
Im trying to build a taxonomy query to only display the terms that have a certain custom field value assigned to it. The custom field holds a boolean value, so I tried:
$args = array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'meta_key' => 'featured',
'meta_value' => true
);
Also:
$args = array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'meta_query' => array(
array(
'key' => 'featured',
'value' => true,
'compare' => '='
)
)
);
None of these returned the expected results. What am I missing?
Share Improve this question edited Jul 2, 2016 at 15:54 birgire 67.8k7 gold badges119 silver badges251 bronze badges asked Jul 2, 2016 at 10:35 Luis MartinsLuis Martins 2351 gold badge2 silver badges12 bronze badges1 Answer
Reset to default 12Inserting boolean term meta values
When we add non-existent term meta with e.g.
add_term_meta( 123, 'test', true );
then we are actually running the following insert :
$wpdb->insert( 'wp_termmeta', array(
'term_id' => 123,
'meta_key' => 'test',
'meta_value' => true
) );
within the general add_metadata()
function.
Now wpdb::insert()
is actually a wrapper for wpdb::_insert_replace_helper()
that prepares the SQL insert query and maps the insert values to:
INSERT INTO `wp_termmeta` (`term_id`, `meta_key`, `meta_value`) VALUES (%d, %s, %s)
or in our test case:
INSERT INTO `wp_termmeta` (`term_id`, `meta_key`, `meta_value`) VALUES (123, 'test', '1')
Also note that the meta_value
column is of longtext type in the wp_termmeta
table.
So the boolean true
is stored as the '1'
string.
Fetching boolean term meta values
When get_terms()
runs with this kind of meta query:
$args = array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'meta_query' => array(
array(
'key' => 'featured',
'value' => true,
'compare' => '='
)
)
);
then the generated SQL query contains:
wp_termmeta.meta_key = 'featured' AND wp_termmeta.meta_value = '1'
where the true
(bool) is converted to the '1'
(string).
本文标签: meta queryUsing getterms() with metaquery parameters
版权声明:本文标题:meta query - Using get_terms() with meta_query parameters 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736300795a1930946.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论