Last updated on September 24, 2023

ACF field group location settings

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Managing ACF Field Group Location Settings.

The ACF (Advanced Custom Fields) plugin allows you to create custom fields for your WordPress website. One of the key features of ACF is the ability to specify the location settings for your field groups. These location settings determine where the custom fields will be displayed on your website.

To set the location settings for an ACF field group, you can use the acf_add_local_field_group() function. This function accepts an array of parameters, including the location parameter, which defines the rules for displaying the field group.

Here’s an example code snippet that demonstrates how to set the location settings for an ACF field group:

function wpsnippets_acf_field_group_location() {
    acf_add_local_field_group(array(
        'key' => 'group_5f8a6e3b1e2d4',
        'title' => 'My Field Group',
        'fields' => array(
            // Define your custom fields here
        ),
        'location' => array(
            array(
                array(
                    'param' => 'post_type',
                    'operator' => '==',
                    'value' => 'post',
                ),
            ),
        ),
    ));
}
add_action('acf/init', 'wpsnippets_acf_field_group_location');

In this example, the acf_add_local_field_group() function is used to create a new field group with the key 'group_5f8a6e3b1e2d4' and the title 'My Field Group'. The fields parameter is where you define your custom fields.

The location parameter is an array that defines the rules for displaying the field group. In this example, the field group will be displayed only on posts. You can customize the location rules based on your specific needs.

By using the acf_add_local_field_group() function and specifying the location settings, you can control where your custom fields are displayed on your WordPress website. This allows you to tailor the editing experience for different post types, pages, or other conditions.

Examples

Example 1: Display ACF field group on specific post types

This use case demonstrates how to use the ACF field group location settings to display a field group on specific post types.

function wpsnippets_acf_field_group_location( $location ) {
    $location['post_type'] = array( 'post', 'page' );
    return $location;
}
add_filter( 'acf/location/rule_values/post_type', 'wpsnippets_acf_field_group_location' );

In this example, we use the acf/location/rule_values/post_type filter to modify the available post types for the ACF field group location settings. By adding the desired post types to the $location['post_type'] array, the field group will only be displayed on those specific post types.

Example 2: Display ACF field group on specific taxonomy terms

This use case demonstrates how to use the ACF field group location settings to display a field group on specific taxonomy terms.

function wpsnippets_acf_field_group_location( $location ) {
    $location['taxonomy'] = array( 'category', 'tag' );
    return $location;
}
add_filter( 'acf/location/rule_values/taxonomy', 'wpsnippets_acf_field_group_location' );

In this example, we use the acf/location/rule_values/taxonomy filter to modify the available taxonomies for the ACF field group location settings. By adding the desired taxonomies to the $location['taxonomy'] array, the field group will only be displayed on posts that are assigned to those specific taxonomy terms.

Example 3: Display ACF field group on specific page templates

This use case demonstrates how to use the ACF field group location settings to display a field group on specific page templates.

function wpsnippets_acf_field_group_location( $location ) {
    $location['page_template'] = array( 'template-about.php', 'template-contact.php' );
    return $location;
}
add_filter( 'acf/location/rule_values/page_template', 'wpsnippets_acf_field_group_location' );

In this example, we use the acf/location/rule_values/page_template filter to modify the available page templates for the ACF field group location settings. By adding the desired page templates to the $location['page_template'] array, the field group will only be displayed on pages that use those specific templates.

Last updated on September 24, 2023. Originally posted on September 21, 2023.

Leave a Reply