Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
(function($) {
acf.add_action('ready', function( $el ){
//Identify the field you want to check against.
//Use a class to handle scenarios involving repeater / flexible content where multiple instances exist
var $field = $('.example input');
$field.on('change', function (evt) {
//Comparing against taxonomy field values to determine which other fields to show / hide
switch($(this).val()) {
case '27':
//A semi-strange "up over down" jQuery to ensure if inside a repeater / flexibl content you show/hide only the desired field
//All other states, lets ensure they are hidden
//On initial page load - ACF data may indicate that a field should be shown instead of hidden
//Each ensures it craws for all repeaters / flexible content
$field.each(function( index ) {
if ($(this).val() == '27') {
add_filter('acf/validate_value/name=conditional-display', 'validate_conditional_display_field', 10, 4);
function validate_conditional_display_field($valid, $value, $field, $input) {
// bail early if value is already invalid
if (!$valid) {
return $valid;
//Loop through each flexible content
foreach ($_POST['acf']['field_5775b7d7f4f20'] as $k => $data) {
//Compare Taxonomy field and whether the 'conditional-display' field is empty
if( $data['field_577aa1d7d9713'] == "27" && !$data['field_5798d4fa12790']):
$valid = __('You have chosen (taxonomy) so you must provide a (conditional display field name).');
return $valid;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment