Support

Account

Home Forums Front-end Issues Custom Location Rules and Group Visibility Short-Circuit

Helping

Custom Location Rules and Group Visibility Short-Circuit

  • Hello,

    I’m trying to map Advanced Custom Field Forms to Front-end Endpoints. I’ve created a new ACF Location Class to map the two. When creating my Advanced Custom Field Form I set the Location for the Post Type and for the Custom Endpoint. I’m using acf_get_field_groups() to get all field groups which match my custom rules:

    $field_groups = acf_get_field_groups( array(
    	'post_type' => 'post',
    	'prefix_endpoint' => 'new',
    ) );

    The above returns all fields matching the post type, regardless if the other rule (prefix_endpoint) is met.

    For example, if I have 1 ACF Field Group to handle the “New Post” form I would set its Post Type to “Post” and its Endpoint Location (custom) to ‘New’. Great, but if I want to create another ACF Field Group that only displays in the admin panel (example: internal note) I would just set its post type to ‘Post’. The problem is that then the above acf_get_field_groups() function returns both these forms. It does not seem to check against prefix_endpoint at all.

    If I use the following code:

    $field_groups = acf_get_field_groups( array(
    	'prefix_endpoint' => 'new',
    ) );

    I get back 0 results because I’m not passing the post_type along with it. Here’s the troublesome code from includes\acf-field-group-functions.php LN458 FN acf_get_field_group_visibility()

    $match_group = true;
    foreach( $group as $rule ) {
    	if( !acf_match_location_rule( $rule, $screen, $field_group ) ) {
    		$match_group = false;
    		break;
    	}
    }

    Once it sees that the rule is not met(post_type), it doesn’t process the other rules which is OK. On the other-hand if I pass additional “rules” to acf_get_field_groups() (such in example 1) it doesn’t check to see if all the passed rules are met which results in multiple returned field groups.

    So just to recap:

    $field_groups = acf_get_field_groups( array(
    	'prefix_endpoint' => 'new',
    ) );

    Returns 0 results because I’m not passing a post type.

    $field_groups = acf_get_field_groups( array(
    	'post_type' => 'post',
    	'prefix_endpoint' => 'new',
    ) );

    Returns multiple results based solely on the post type without checking against prefix_endpoint rule.

    Is there a different method other than acf_get_field_groups() which checks to ensure all passed rules are met?

  • The way that ACF locations rules work you must pass all of the rules you want met, or set them when creating a group. It does not check rules that are not set.

    If you have a rule set for a field group that of post type is equal to post this is all that will be checked.

    To check another rule the location rule needs to be in the rules for that group, for example prefix end point does not equal new

    As an example create a field group and set the location rules of the group to current user is equal to logged in. When you are logged in this group will appear on all admin pages where ACF is able to add field group. It does not, for example, test to see if is should be on the post edit page or the user profile page.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.