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 🙂
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.