Support

Account

Home Forums Backend Issues (wp-admin) Custom Location Rule for Custom Taxonomy Reply To: Custom Location Rule for Custom Taxonomy

  • Hi @brettzie

    I believe you can do it by using the get_terms() function to make it simpler. Maybe something like this:

    add_filter('acf/location/rule_types', 'acf_location_rules_types_product');
    
    function acf_location_rules_types_product( $choices ) {
        
        $choices['Post']['product_tax'] = 'Product Tax';
    
        return $choices;
        
    }
    
    add_filter('acf/location/rule_values/product_tax', 'acf_location_rules_values_product');
    
    function acf_location_rules_values_product( $choices ) {
        
        $terms = get_terms( array(
            'taxonomy' => 'fr_product_categories',
            'hide_empty' => false,
        ) );
    
        if( $terms ) {
            
            foreach( $terms as $term ) {
                
                $choices[ $term->term_id ] = $term->name;
                
            }
            
        }
    
        return $choices;
    }
    
    add_filter('acf/location/rule_match/product_tax', 'acf_location_rules_match_product_category', 10, 3);
    
    function acf_location_rules_match_product_category($match, $rule, $options){
        // validate
        if(!$options['post_id']){return false;}
        
        // vars
        $terms = get_terms( array(
            'taxonomy' => 'fr_product_categories',
            'hide_empty' => false,
        ) );
            
        //print_r($options);
        //print_r($rule);
        
        if($rule['operator'] == "==")
        {
            $match = in_array( $rule['value'], $options['post_taxonomy']);
        }
        elseif($rule['operator'] == "!=")
        {
            $match = !in_array( $rule['value'], $options['post_taxonomy']);
        }
    
        return $match;
    
    }

    I hope this helps πŸ™‚