Support

Account

Home Forums General Issues Filter WooCommerce categories by ACF field

Unread

Filter WooCommerce categories by ACF field

  • I have an ACF checkbox on woocommerce categories homepage_featured and am trying to get a list of all categories which have this checkbox ticked. I have tried using the below code but have had no luck:

    function shortcode_handler() {
          $output;
    
          $args = array(
            'taxonomy' => 'product_cat',
            'hide_empty' => 0
          );
          $c = get_categories($args);
          $c_keep = array();
          foreach($c as  $cat){
            if (get_field('homepage_featured', 'category_'.$cat->term_id)) {
              $c_keep[] = $cat; // forgot [] here
            }
          }
    
          foreach($c_keep as $cat){
            $ouput .= $cat->name;
          }
    
          return $output;
         }
        add_shortcode('featured_categories','shortcode_handler');
Viewing 1 post (of 1 total)

The topic ‘Filter WooCommerce categories by ACF field’ is closed to new replies.