Home › Forums › General Issues › Custom Location Rules – Works Only on Ajax Call › Reply To: Custom Location Rules – Works Only on Ajax Call
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;
}
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’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U
— Advanced Custom Fields (@wp_acf) May 22, 2023
© 2023 Advanced Custom Fields.
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.