Skip Navigation

[Resolved] Filtering Taxonomy Terms to show in Select Field in CRED form

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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: Asia/Karachi (GMT+05:00)

Author
Posts
#2621243

Tell us what you are trying to do?

I have a hierarchical taxonomy of Locations with the hierarchy being Continent > Region > Country. The hierarchy is useful for frontend search, however when submitting a new post using a CRED form I want the user to only be able to select "Country" level terms, not the regions or continents.

Is there any documentation that you are following?

I have implemented the code from this forum post: https://toolset.com/forums/topic/how-to-display-only-certain-taxonomy-terms-on-cred-form/

It works as advertised, and I only see the terms that are selected. However (as also mentioned in the forum thread) in order for a term to be visible all parents and grand parents must be visible, which defeat my purpose.

Is there a similar example that we can see?

Here is my actual code:

// Alter the query to only get the terms where the custom field use-in-form checked.
function wp_custom_get_terms_args( $args, $taxonomies ) {
error_log('wp_custom_get_terms_args');

$args['meta_query'] = array(
array(
'key' => 'wpcf-selectable',
'value' => '1',
'compare' => '='
),
);

return $args;
}

// Add the query filter for our form.
add_action('toolset_forms_frontend_flow_form_start', function($form_object, $form_attributes){
$forms = array(8124);

if ( in_array( $form_object->ID, $forms ) ) {
add_filter( 'get_terms_args', 'wp_custom_get_terms_args', 10, 2 );
}
}, 10, 2);

// remove the query filter for the other parts of the website
add_action('toolset_forms_frontend_flow_form_end', function(){
remove_filter( 'get_terms_args', 'wp_custom_get_terms_args');
}, 10, 2);

I wonder if there is another method I could use to achieve the same goal of filtering the Select Options?

#2621253

Hi,

Thank you for contacting us and I'd be happy to assist.

For what you're planning, you can use a different approach.

1. Instead of using the regular taxonomy field in the form, you can add a generic field and then fill its options programmatically.

2. To generate the options of this generic field, you can use a custom shortcode that populates the options by getting only the third level of taxonomy terms (i.e. Countries).

Here are some useful links:
https://developer.wordpress.org/reference/functions/get_terms/
https://toolset.com/forums/topic/in-a-view-only-show-children-taxonomy-for-all-parents/

3. On the form's submission, you can process the selected taxonomy terms and attach them to the created/edited post, through a custom function attached to the 'cred_save_data'
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

I hope this helps and please let me know if you need further assistance.

regards,
Waqar