Last updated on September 14, 2023

ACF conditional logic not working in WordPress

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

Troubleshooting ACF Conditional Logic in WordPress.

ACF (Advanced Custom Fields) is a popular plugin in WordPress that allows you to easily create custom fields for your posts, pages, and custom post types. One of its powerful features is conditional logic, which allows you to show or hide fields based on certain conditions.

However, sometimes the conditional logic in ACF may not work as expected. This can happen due to various reasons, such as incorrect field names, incorrect operator usage, or conflicts with other plugins or themes.

To troubleshoot and fix ACF conditional logic issues, you can follow these steps:

  1. Verify field names: Double-check the field names used in your conditional logic rules. Make sure they match the actual field names in your ACF group settings. A common mistake is using incorrect field names, which can cause the conditional logic to fail.

  2. Check operator usage: ACF provides various operators like “equals”, “not equals”, “contains”, etc., for conditional logic. Ensure that you are using the correct operator for your specific condition. For example, if you want to check if a field value is equal to a specific value, use the “equals” operator.

  3. Test with simple conditions: Start with simple conditions to isolate the issue. For example, create a simple rule to show or hide a field based on a static value. If this works, gradually add more complex conditions to identify any specific condition causing the issue.

  4. Disable conflicting plugins or themes: Sometimes, conflicts with other plugins or themes can interfere with ACF’s conditional logic. Temporarily disable other plugins or switch to a default theme (like Twenty Twenty-One) to see if the issue persists. If the conditional logic works with the conflicting plugin/theme disabled, you may need to investigate further or contact the plugin/theme developer for a resolution.

Here’s an example of how to use ACF conditional logic to show or hide a field based on the value of another field:

<?php
function wpsnippets_acf_conditional_logic_example() {
    if ( function_exists( 'acf_add_local_field_group' ) ) {
        acf_add_local_field_group( array(
            'key'      => 'group_123456789',
            'title'    => 'Conditional Logic Example',
            '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' => '==',
                                'value'    => 'example',
                            ),
                        ),
                    ),
                ),
            ),
            'location' => array(
                array(
                    array(
                        'param'    => 'post_type',
                        'operator' => '==',
                        'value'    => 'post',
                    ),
                ),
            ),
        ) );
    }
}
add_action( 'acf/init', 'wpsnippets_acf_conditional_logic_example' );
?>

In this example, we define a custom field group with two text fields: “Field 1” and “Field 2”. The conditional logic is set on “Field 2” to show it only when the value of “Field 1” is equal to “example”. The field group is then assigned to the “post” post type.

By following the steps mentioned above and using the provided code example, you can troubleshoot and fix ACF conditional logic issues in WordPress.

Examples

Example 1: ACF Conditional Logic with Radio Button Field

This example demonstrates how to use ACF (Advanced Custom Fields) conditional logic with a radio button field in WordPress. The conditional logic will show or hide a specific field based on the selected value of the radio button.

<?php
function wpsnippets_acf_conditional_logic_radio() {
    if ( function_exists( 'acf_add_local_field_group' ) ) {
        acf_add_local_field_group( array(
            'key' => 'group_1',
            'title' => 'Conditional Logic Example',
            'fields' => array(
                array(
                    'key' => 'field_1',
                    'label' => 'Show Field',
                    'name' => 'show_field',
                    'type' => 'radio',
                    'choices' => array(
                        'yes' => 'Yes',
                        'no' => 'No',
                    ),
                ),
                array(
                    'key' => 'field_2',
                    'label' => 'Hidden Field',
                    'name' => 'hidden_field',
                    'type' => 'text',
                    'conditional_logic' => array(
                        array(
                            'field' => 'field_1',
                            'operator' => '==',
                            'value' => 'yes',
                        ),
                    ),
                ),
            ),
            'location' => array(
                array(
                    array(
                        'param' => 'post_type',
                        'operator' => '==',
                        'value' => 'post',
                    ),
                ),
            ),
        ) );
    }
}
add_action( 'acf/init', 'wpsnippets_acf_conditional_logic_radio' );
?>

In this code example, we define a custom field group using the acf_add_local_field_group function. The field group contains two fields: a radio button field (show_field) and a hidden text field (hidden_field). The conditional_logic parameter is used to specify that the hidden_field should be shown only when the value of show_field is set to “Yes”. The field group is then assigned to the “post” post type.

Example 2: ACF Conditional Logic with Checkbox Field

This example demonstrates how to use ACF conditional logic with a checkbox field in WordPress. The conditional logic will show or hide a specific field based on the selected values of the checkbox.

<?php
function wpsnippets_acf_conditional_logic_checkbox() {
    if ( function_exists( 'acf_add_local_field_group' ) ) {
        acf_add_local_field_group( array(
            'key' => 'group_2',
            'title' => 'Conditional Logic Example',
            'fields' => array(
                array(
                    'key' => 'field_3',
                    'label' => 'Show Field',
                    'name' => 'show_field',
                    'type' => 'checkbox',
                    'choices' => array(
                        'option_1' => 'Option 1',
                        'option_2' => 'Option 2',
                    ),
                ),
                array(
                    'key' => 'field_4',
                    'label' => 'Hidden Field',
                    'name' => 'hidden_field',
                    'type' => 'text',
                    'conditional_logic' => array(
                        array(
                            'field' => 'field_3',
                            'operator' => '==',
                            'value' => 'option_1',
                        ),
                    ),
                ),
            ),
            'location' => array(
                array(
                    array(
                        'param' => 'post_type',
                        'operator' => '==',
                        'value' => 'page',
                    ),
                ),
            ),
        ) );
    }
}
add_action( 'acf/init', 'wpsnippets_acf_conditional_logic_checkbox' );
?>

In this code example, we define another custom field group using the acf_add_local_field_group function. The field group contains a checkbox field (show_field) and a hidden text field (hidden_field). The conditional_logic parameter is used to specify that the hidden_field should be shown only when the value of show_field contains “Option 1”. The field group is then assigned to the “page” post type.

Example 3: ACF Conditional Logic with Select Field

This example demonstrates how to use ACF conditional logic with a select field in WordPress. The conditional logic will show or hide a specific field based on the selected option of the select field.

<?php
function wpsnippets_acf_conditional_logic_select() {
    if ( function_exists( 'acf_add_local_field_group' ) ) {
        acf_add_local_field_group( array(
            'key' => 'group_3',
            'title' => 'Conditional Logic Example',
            'fields' => array(
                array(
                    'key' => 'field_5',
                    'label' => 'Show Field',
                    'name' => 'show_field',
                    'type' => 'select',
                    'choices' => array(
                        'option_1' => 'Option 1',
                        'option_2' => 'Option 2',
                    ),
                ),
                array(
                    'key' => 'field_6',
                    'label' => 'Hidden Field',
                    'name' => 'hidden_field',
                    'type' => 'text',
                    'conditional_logic' => array(
                        array(
                            'field' => 'field_5',
                            'operator' => '==',
                            'value' => 'option_2',
                        ),
                    ),
                ),
            ),
            'location' => array(
                array(
                    array(
                        'param' => 'post_type',
                        'operator' => '==',
                        'value' => 'post',
                    ),
                ),
            ),
        ) );
    }
}
add_action( 'acf/init', 'wpsnippets_acf_conditional_logic_select' );
?>

In this code example, we define another custom field group using the acf_add_local_field_group function. The field group contains a select field (show_field) and a hidden text field (hidden_field). The conditional_logic parameter is used to specify that the hidden_field should be shown only when the value of show_field is set to “Option 2”. The field group is then assigned to the “post” post type.

Last updated on September 14, 2023. Originally posted on September 15, 2023.

Leave a Reply

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