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 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.