Advanced Custom Fields: Using category as a rule, before publishing new post?

Another option, apart of the solution linked by @Milo, would be to remove the category rule from the field group, and control the visibility of this ACF group with jQuery.

So, assuming that the Meta Box with the ACF fields has the id #acf_79 and that you want it assigned to the category with ID 1, that is rendered as #in-category-1 in the category Meta Box, the following will do.
Use the browser inspector to get the correct ID’s.

add_action( 'admin_footer-post.php', 'conditional_acf_metabox_wpse_78772' );
add_action( 'admin_footer-post-new.php', 'conditional_acf_metabox_wpse_78772' );

function conditional_acf_metabox_wpse_78772() 
{
    // Not our post type, do nothing. Adjust if using another post_type.
    global $current_screen;
    if ( 'post' != $current_screen->post_type ) 
        return; 

    ?>
    <script type="text/javascript">
    jQuery(document).ready( function($) 
    {    
        // For a smoother effect, hide the box with CSS and show it if "is(':checked')"
        if( ! $('#in-category-1').attr('checked')  )
            $('#acf_79').hide();

        // Watch the behavior of Category 1 checkbox
        $('#in-category-1').change( function () 
        {       
            // Show/Hide the ACF meta box   
            if( $(this).is(':checked') )
            {
                $('#acf_79').slideDown();
            }
            else
            {
                $('#acf_79').slideUp();
            }
        });
    });
    </script>
    <?php
}

Leave a Comment