Skip Navigation

[Resolved] Hide some select field options based on condition

This support ticket is created 3 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+00:00)

This topic contains 7 replies, has 2 voices.

Last updated by nabils 3 years, 3 months ago.

Assisted by: Jamal.

Author
Posts
#1882247

Hi,

I am using a radio field with some options like pending, approved, rejected and other. I want to add condition such as if it was approved then hide rejected. Is this possible?

#1882565

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hello and thank you for contacting the Toolset support.

Currently, there is no way to hide some options from a field based on a condition. Conditions will work for the whole field and not for its options.

You may probably implement this by creating two radio fields, one that holds the 3 options and one that holds only two options, and based on a condition you can display one of the fields. You may also want to add custom code that will sync values from one field to another.

I hope this helps. Let me know if you have any questions.

#1884201

Yes, I will use two fields then. Could you advise how would be the custom code to show that it is only one field?

#1886557

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

The custom code-based solution that I suggest, is not to show that is only one field. I suggested the custom code to synchronize the value from the second field into the first.

So let's say that we have a radio field "status" that has possible options (pending, approved, rejected). We can add a second field, let's call it "status-2" that will have 2 options (pending, approved).
We can then build two conditions, one to display "status" if its value is different than "approved", the second will display "status-2" if the value of "status" equals "approved".
At this point, there is no need for any custom code. But, I imagine that you want to use these fields in a form, or that you just want to rely only on the value of "status", and that when we display "status-2", in a form or in the backend, and its value has changed, you would want to synchronize its value to "status", right! In that case, we'll need a custom code to do that. We can rely on the "save_post" hook and synchronize the value. Here is an example code:

add_action( 'save_post', 'my_prefix_sync_status', 10,3 );
 
function set_post_default_category( $post_id, $post, $update ) {
    // Get the value from "status-2"
    $status = get_post_meta( $post_id, 'wpcf-status-2', true);

    // Update the value in "status"
    update_post_meta( $post_id, 'wpcf-status', $status );
}

More about the hook and the used functions in the links below:
- https://developer.wordpress.org/reference/hooks/save_post/
- https://developer.wordpress.org/reference/functions/get_post_meta/
- https://developer.wordpress.org/reference/functions/update_post_meta/

Note that Toolset adds a prefix("wpcf-") to the fields slugs("status", and "status-2").
https://toolset.com/documentation/customizing-sites-using-php/functions/

#1887599

Thank you for the explanation. I am trying to make two status fields. Status 1 (pending, approved, decline). Status 2 ( borrowed, returned). In the beginning status 1 filed will be shown. When status 1 changed to approved, status 2 will be shown. I tried to use below condition but it is not working. Please let me know how to make it work.

[wpv-conditional if="($(enginvrequests-status-1) eq 'Pending')" mode="fade-slide"]

[cred_field field='enginvrequests-status-1' post='enginvrequest' value='' urlparam=''] [/wpv-conditional]
[wpv-conditional if="($(enginvrequests-status-1) ne 'Pending')" mode="fade-slide"]

[cred_field field='enginvrequests-status-2' post='enginvrequest' value='' urlparam=''] [/wpv-conditional]

New threads created by Jamal and linked to this one are listed below:

https://toolset.com/forums/topic/conditions-on-radio-field-do-not-work-in-a-form/

#1887601

I am writing the condition in the post form

#1887863

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

For support rules, we are able to handle only one issue at a time. This helps us to bring you a better service and also helps other users to find all the information here exposed. Because your last question is a different one than the original one of this ticket, I split this ticket into a new one and I'll be replying there.

#1889117

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.