Yes, we can pull this off and let your client choose what terms to display in the form. We'll add a checkbox custom field to terms. Your client will need to select this checkbox on all the terms that he would want to have in the forms. Then we'll add a custom code that will get only the checked terms. This way, your client won't have to edit the custom code when he wants to add or remove terms from the forms. Does it make sense?
So I run a small test and I was able to get only the selected terms. I worked on the default Taxonomy "Categories" and the default Post type. You can log into my test site with the following URL hidden link
These are the steps I followed:
1. Create a custom term field (Use In Form). Check these screenshots hidden link and hidden link
2. Create a couple of terms, and select the check box on them. Check this screenshot hidden link
3. Create a post form and add the taxonomy(Categories) field to it.
4. Here comes the custom code part. We will pull only the terms that have the checkbox filed selected. First, we add a function to alter the terms query:
// 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-use-in-form',
'value' => '1',
'compare' => '='
),
);
return $args;
}
Then we need to use this function only for our form(s), we'll use the undocumented action toolset_forms_frontend_flow_form_start:
// Add the query filter for our form.
add_action('toolset_forms_frontend_flow_form_start', function($form_object, $form_attributes){
$forms = array(15);
if ( in_array( $form_object->ID, $forms ) ) {
add_filter( 'get_terms_args', 'wp_custom_get_terms_args', 10, 2 );
}
}, 10, 2);
We add the forms IDs in the line 3.
Then, we need to remove the hook, so other parts of the website pull all the terms, for example the widget at the bottom of the page.
// 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);
And that gives us the expected results, you can check it on this page hidden link
As you can see on this screenshot hidden link The form will display only the selected terms, but the widget will display all the terms that have a published post assigned.
Here are all the elements involved:
- Term custom fields: hidden link
- The form(ID: 15): hidden link
- The custom code: hidden link
I hope this helps. Let me know if you have any questions.