Home › Forums › Backend Issues (wp-admin) › ACF Custom Locations don't work › Reply To: ACF Custom Locations don't work
The “§ruleS” was just a spelling mistake in the topic, I did use the original array name in the code.
I did write an own code, but I now used the snippet from the documentation. The exact code is:
add_filter('acf/location/rule_types', 'acf_location_rules_types');
function acf_location_rules_types( $choices )
{
$choices['Basic']['user'] = 'User';
return $choices;
}
add_filter('acf/location/rule_operators', 'acf_location_rules_operators');
function acf_location_rules_operators( $choices )
{
$choices['<'] = 'is less than';
$choices['>'] = 'is greater than';
return $choices;
}
add_filter('acf/location/rule_values/user', 'acf_location_rules_values_user');
function acf_location_rules_values_user( $choices )
{
$users = get_users();
if( $users )
{
foreach( $users as $user )
{
$choices[ $user->data->ID ] = $user->data->display_name;
}
}
return $choices;
}
add_filter('acf/location/rule_match/user', 'acf_location_rules_match_user', 10, 3);
function acf_location_rules_match_user( $match, $rule, $options )
{
$current_user = wp_get_current_user();
$selected_user = (int) $rule['value'];
if($rule['operator'] == "==")
{
$match = ( $current_user->ID == $selected_user );
}
elseif($rule['operator'] == "!=")
{
$match = ( $current_user->ID != $selected_user );
}
return $match;
}
The code is placed at the very bottom of my theme’s functions.php to ensure that it isn’t executed too early.
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!
CPT registration is coming to ACF! We demoed the new feature during the most recent session of ACF Chat Fridays. Check out the summary for the details. https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 7, 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.