admin管理员组文章数量:1323707
Im trying to build a page template on wordpress that will show a list a categories that have a custom field checked.
So the taxonomy is just the default woocommerce taxonomy product_cat
I then have a custom field to the product_cat taxonomy which is a check box called collectable
My set up is Main Category
- Child
- Child
- Child
- Child
So for example 2 children categories have the tick box collectable
which is set to add the value of 1 into the datbase
So I am doing a page where it will show all categories with the collectable checked.
$args = array(
'post-type' => 'product',
'taxonomy' => 'product_cat',
'hide_empty' => 0
);
$c = get_categories($args);
$c_keep = array();
foreach($c as $cat){
if (get_field('collectable', 'category_'.$cat->term_id)) {
$c_keep[] = $cat;
}
}
foreach($c_keep as $cat){
echo $cat->name;
}
But I am getting nothing at all returned.
I even put a
print_r( $args );
But I am still coming up with a blank (Header and footer Loads and the text above the query)
Can anyone help please
****** EDIT *****
Did as suggest to run a meta query
<?php
$terms = get_terms(
array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'meta_key' => 'collectable',
'meta_value' => '1',
)
);
$terms = get_terms( $args );
foreach($terms as $cat){ ?>
<?php echo $cat->name; ?> <BR>
<?php
}
print_r( $terms );
?>
This just bought back all categories whether they have the mata_value or not
Im trying to build a page template on wordpress that will show a list a categories that have a custom field checked.
So the taxonomy is just the default woocommerce taxonomy product_cat
I then have a custom field to the product_cat taxonomy which is a check box called collectable
My set up is Main Category
- Child
- Child
- Child
- Child
So for example 2 children categories have the tick box collectable
which is set to add the value of 1 into the datbase
So I am doing a page where it will show all categories with the collectable checked.
$args = array(
'post-type' => 'product',
'taxonomy' => 'product_cat',
'hide_empty' => 0
);
$c = get_categories($args);
$c_keep = array();
foreach($c as $cat){
if (get_field('collectable', 'category_'.$cat->term_id)) {
$c_keep[] = $cat;
}
}
foreach($c_keep as $cat){
echo $cat->name;
}
But I am getting nothing at all returned.
I even put a
print_r( $args );
But I am still coming up with a blank (Header and footer Loads and the text above the query)
Can anyone help please
****** EDIT *****
Did as suggest to run a meta query
<?php
$terms = get_terms(
array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'meta_key' => 'collectable',
'meta_value' => '1',
)
);
$terms = get_terms( $args );
foreach($terms as $cat){ ?>
<?php echo $cat->name; ?> <BR>
<?php
}
print_r( $terms );
?>
This just bought back all categories whether they have the mata_value or not
Share Improve this question edited Sep 8, 2020 at 16:00 user1348927 asked Sep 8, 2020 at 15:16 user1348927user1348927 111 silver badge6 bronze badges 5 |1 Answer
Reset to default 0The meta is within the taxonomy (so I have the standard, category name, slug, description etc and then I added a collectable tickbox to the category itself not the post/product)
I solved this by Running a query for all sub categories and then I put an if statement in so it shows only categories with the collectable box ticked.
(used toolset for the custom field in the end)
$parent_id = 16;
$args = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' => $parent_id,
'taxonomy' => 'product_cat'
);
$subcategories = get_categories($args);
);
foreach ($subcategories as $category) {
$st_var = $category->to_array();
$thumbnail_id = types_render_termmeta("collectable",array("term_id" =>$st_var['term_id']));
if ( $thumbnail_id ) {
/// Put my html here, category name, image, link etc
}
}
本文标签: customizationQuery Product categories what have a custom field
版权声明:本文标题:customization - Query Product categories what have a custom field 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742129385a2422087.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
get_field
is an ACF function, you wantget_term_meta
for a terms meta fields, andget_post_meta
for a posts meta fields. It's unclear though how you've added thecollectable
field, and what you've added it to – Tom J Nowell ♦ Commented Sep 8, 2020 at 16:41