Home › Forums › Backend Issues (wp-admin) › Conditional Field / Different Field Group
I want a field in one field group to be conditional based on a true/false toggle field set in a different field group.
Is this possible? I tried but it says “No toggle fields available”
I’m assuming this is because my toggle field is in another group.
The purpose being I want a set of toggles that show only for the Administrator, that enable/disable certain fields for members.
For anyone still looking for this, it is may be possible, but it will be a bit of work. You’ll need to add an acf/load_field filter for each of the fields. In the load field filter your will need to construct the needed conditional logic using the correct field keys and values from the fields you want to use. I’m not 100% sure this should work and I have not tested it, but there’s no reason that I know of that it shouldn’t work. For examples of what conditional logic needs to look like create some fields using it and then export them. Any field values that’s listed when you export a field can be modified in load field filter.
Hope this help anyone that’s looking to be able to do something like this.
Looks similar to https://support.advancedcustomfields.com/forums/topic/conditional-logic-across-field-groups/ see my answer below this topic it should help.
It must have been right after this that I created https://github.com/Hube2/acf-custom-field-locations-rule, it works with ACF Pro. It takes a slightly different approach. I didn’t even know that the page you linked to existed.
I ran into a need for this today. This forum post seems to be a top google hit for my search so I’ll put the solution that worked for me here.
The filter mentioned by John up above wasn’t able to influence field keys that were not a part of the field group of the field I was targeting.
I ended up needing to use some of the JS API functions for this. https://www.advancedcustomfields.com/resources/javascript-api/
In the example code below, I had a button group field with two options, and depending on the chosen option I am showing or hiding a field from another field group.
There are a few ways to get the field keys, I found the easiest way was just to inspect the HTML on the page, but you could also get them from the GUI in the field group builder, or the ACF JSON files if you are using that.
// Handle show/hide on ready
acf.addAction( 'ready', function() {
buttonField = acf.getField( 'field_6202e1499e575' );
conditionalField = acf.getField( 'field_5e98b383fa39f' );
switch( buttonField.val() ) {
case 'traditional':
conditionalField.show();
break;
case 'simplified':
conditionalField.hide();
break;
}
});
// Handle show/hide when value is changed
$('input[name="acf[field_6202e1499e575]"]').on( 'change', function() {
conditionalField = acf.getField( 'field_5e98b383fa39f' );
switch( $( this ).val() ) {
case 'traditional':
conditionalField.show();
break;
case 'simplified':
conditionalField.hide();
break;
}
});
// I didn't need any of either of these callbacks, but I verified they do get called with the above code
function showFieldCallback( field ) {
console.log( 'show callback fired' );
}
acf.addAction( 'show_field/key=field_5e98b383fa39f', showFieldCallback );
function hiddenFieldCallback( field ) {
console.log( 'hidden callback fired' );
}
acf.addAction( 'hide_field/key=field_5e98b383fa39f', hiddenFieldCallback );
I hope this helps someone else!
The topic ‘Conditional Field / Different Field Group’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.