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)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.