Home › Forums › General Issues › Custom Location Rules – Works Only on Ajax Call
Hi. First let me say I love this plugin and worth every money for the add-ons!
Ok so I added my own custom location rule as I want my ACF fields to appear when a category or any one of its subcategories is selected.
I added 2 functions, one is the operator and another is the matching rule.
add_filter('acf/location/rule_operators', 'acf_location_rules_operators');
function acf_location_rules_operators( $choices )
{
$choices['<='] = '<= (post cat only)';
return $choices;
}
and here’s the matching rule
add_filter('acf/location/rule_match/post_category', 'acf_location_rules_match_post_category', 10, 3);
function acf_location_rules_match_post_category( $match, $rule, $options )
{
$current_cat = $options['post_category']; // array, user selection
$rule_cat_id = (int) $rule['value']; // numeric, rule
if($rule['operator'] == "<=")
{
$match = false;
$match = in_array($rule_cat_id, $current_cat); //check if main cat
if (!$match){
// check the subcategories instead so first get the subcats
$args=array(
'child_of'=>$rule_cat_id,
'hide_empty'=>0
);
$subcats = get_categories($args);
foreach ($subcats as $subcat){
if (in_array($subcat->term_id, $current_cat))
{
$match = true;
break;
}
}
}
}
return $match;
}
Okay it works wonderfully now. I’ll add a new post and if I select the category or its subcategory the fields will appear.
The problem now is, there are already posts created before this and when I edit them, the ACF fields does not appear. I have to uncheck and check the category to get them appearing.
I am hoping that I don’t have to ask my client to do this though. Is there anything I need to do to my code?
Hi @mister_bee
I’m not sure why your location rule is not matching previously saved posts.
Have you debugged the code to make sure it is running on the page load / matching the correct values?
hi @elliot, if I just flag $match as true, the ACF fields appears on the page meaning it gets run during page load but somehow, the rule didn’t match.
is there anyway I can see what is the content of the $options array during page load?
during page load, the $options is an array with zero elements. so I modified my code and now it works.
add_filter('acf/location/rule_match/post_category', 'acf_location_rules_match_post_category', 10, 3);
function acf_location_rules_match_post_category( $match, $rule, $options )
{
$current_cat = $options['post_category']; // array, user selection
$rule_cat_id = (int) $rule['value']; // numeric, rule
global $pagenow;
if (count($current_cat)==0 && $pagenow=='post.php'){
// if we're editing post and not adding a new post
$current_cat= wp_get_post_categories($options["post_id"]);
}
if($rule['operator'] == "<=")
{
$match = false;
$match = in_array($rule_cat_id, $current_cat); //check if main cat
if (!$match){
// check the subcategories instead so first get the subcats
$args=array(
'child_of'=>$rule_cat_id,
'hide_empty'=>0
);
$subcats = get_categories($args);
foreach ($subcats as $subcat){
if (in_array($subcat->term_id, $current_cat))
{
$match = true;
break;
}
}
}
}
return $match;
}
The topic ‘Custom Location Rules – Works Only on Ajax Call’ is closed to new replies.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.