Support

Account

Home Forums General Issues Query Product categories what have a custom field

Helping

Query Product categories what have a custom field

  • Hello

    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

  • Does $c have any value before you start filtering? Are you getting any categories to begin with?

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.