0

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

5
  • You should use a meta query to things like this. Sep 8, 2020 at 15:26
  • How that would work? Because the page needs to show sub categories with "collectable" checked, not a list of products. Sep 8, 2020 at 15:28
  • 1
    @user1348927 Checkout the docs and example of WP_Term_Query
    – Howdy_McGee
    Sep 8, 2020 at 15:42
  • Did as suggested, It brought back all categories. (It should be bringing back 3 currently and it returned all 65 categories.) Sep 8, 2020 at 16:00
  • 2
    get_field is an ACF function, you want get_term_meta for a terms meta fields, and get_post_meta for a posts meta fields. It's unclear though how you've added the collectable field, and what you've added it to
    – Tom J Nowell
    Sep 8, 2020 at 16:41

1 Answer 1

0

The 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

    }
} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.