Last updated on September 25, 2023

ACF conditional logic with multiple fields

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

Implementing Conditional Logic with Multiple ACF Fields.

The ACF (Advanced Custom Fields) plugin in WordPress allows you to create custom fields for your posts, pages, or custom post types. One powerful feature of ACF is conditional logic, which allows you to show or hide fields based on certain conditions. In this case, we’ll look at how to use ACF conditional logic with multiple fields.

To use ACF conditional logic with multiple fields, you need to define the conditions for each field using the ACF field group settings. Here’s an example code snippet that demonstrates how to achieve this:

function wpsnippets_acf_conditional_logic() {
    acf_add_local_field_group(array(
        'key' => 'group_1',
        'title' => 'Conditional Fields',
        'fields' => array(
            array(
                'key' => 'field_1',
                'label' => 'Field 1',
                'name' => 'field_1',
                'type' => 'text',
            ),
            array(
                'key' => 'field_2',
                'label' => 'Field 2',
                'name' => 'field_2',
                'type' => 'text',
                'conditional_logic' => array(
                    array(
                        array(
                            'field' => 'field_1',
                            'operator' => '!=empty',
                        ),
                    ),
                ),
            ),
            array(
                'key' => 'field_3',
                'label' => 'Field 3',
                'name' => 'field_3',
                'type' => 'text',
                'conditional_logic' => array(
                    array(
                        array(
                            'field' => 'field_1',
                            'operator' => '==empty',
                        ),
                    ),
                ),
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'post_type',
                    'operator' => '==',
                    'value' => 'post',
                ),
            ),
        ),
    ));
}
add_action('acf/init', 'wpsnippets_acf_conditional_logic');

In this example, we define a field group with three fields: Field 1, Field 2, and Field 3. Field 2 is set to be visible only when Field 1 is not empty, while Field 3 is set to be visible only when Field 1 is empty. The conditional_logic parameter is used to define these conditions.

You can modify this code snippet to fit your specific needs by changing the field keys, labels, names, types, and conditions. Remember to replace the 'post_type' => 'post' condition in the 'location' parameter with the appropriate post type(s) where you want these fields to appear.

This code snippet can be useful when you want to create dynamic forms or content editing interfaces where certain fields should be shown or hidden based on the values of other fields. It allows you to create more interactive and user-friendly experiences for managing custom data in WordPress.

Examples

Example 1: Conditional Logic with Multiple Fields

This use case demonstrates how to implement conditional logic using multiple fields in Advanced Custom Fields (ACF). The code example shows how to display a text field only if a checkbox field is checked.

<?php
if (get_field('checkbox_field')) {
    echo get_field('text_field');
}
?>

In this code example, we use the get_field() function to retrieve the value of the checkbox field. If the checkbox is checked, we then use the same function to retrieve and display the value of the text field.

Example 2: Conditional Logic with Multiple Fields and Operators

This use case demonstrates how to use operators in conditional logic with multiple fields in ACF. The code example shows how to display a text field only if both a checkbox field and a select field meet certain conditions.

<?php
if (get_field('checkbox_field') && get_field('select_field') === 'option1') {
    echo get_field('text_field');
}
?>

In this code example, we use the && operator to check if both the checkbox field is checked and the select field’s value is equal to 'option1'. If both conditions are met, we then use the get_field() function to retrieve and display the value of the text field.

Example 3: Conditional Logic with Multiple Fields and OR Operator

This use case demonstrates how to use the OR operator in conditional logic with multiple fields in ACF. The code example shows how to display a text field if either a checkbox field is checked or a radio field has a specific value.

<?php
if (get_field('checkbox_field') || get_field('radio_field') === 'option1') {
    echo get_field('text_field');
}
?>

In this code example, we use the || operator to check if either the checkbox field is checked or the radio field’s value is equal to 'option1'. If either condition is met, we then use the get_field() function to retrieve and display the value of the text field.

Last updated on September 25, 2023. Originally posted on October 7, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *