Hi there.
Possibly this ticket should be assigned to Jamal as he has helped another user with this task and likely has experience with the issue.
I am following https://toolset.com/forums/topic/how-to-display-only-certain-taxonomy-terms-on-cred-form/ to filter a CRED Taxonomy Selector, in order to show only few desired terms in the selector.
That ticket suggests to add a filter to get_terms_args() using the toolset_forms_frontend_flow_form_start() action.
That code works just fine, as long we do not pass any ID to $args['parent'].
It works for all other arguments of get_terms, but not for $args['parent'].
In a simple get_terms, we get all child terms of X like so:
get_terms('a-tax',array('parent' => 21 , 'hide_empty'=> '0' ));
Or
get_terms('a-tax',array('child_of' => 21 , 'hide_empty'=> '0' ));
This will nicely returns all terms child to 21.
However, the very same code in the CRED hook used on the ticket mentioned, returns no terms at all, unless we set parent to 0, then it correctly returns all Parent terms!
I made extensive tests, and the only args that do not work are parent and child_of when passed to the toolset_forms_frontend_flow_form_start.
I start suspecting that this has something to do with CRED building the hierarchy of the taxonomy selectors, but dumping the $args I can see my settings passed just fine!
So I am at a loss as of why this does not work.
It is crucial for this specific project that we can filter the taxonomy selector by parents.
This is the code I have (but basically Jamal's example is just fine, you just need to swap his meta query for parent 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 ) {
$args['parent'] = 0;//This works
//$args['parent'] = 21;//This works NOT
//$args['child_of'] = 21;//This works NOT
return $args;
}
// Add the query filter for our form.
add_action('toolset_forms_frontend_flow_form_start', function($form_object, $form_attributes){
$forms = array(5);
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);
Going crazy about why this does not work, specially considering that all other arguments I pass work just fine....
PS:
I also tried passing 'hide_empty'=> '0' just to be sure all terms - even if empty - are considered.
And yet, still the same results.
Dumping the args, as mentioned, I can see the arguments passed just fine!
It is a mystery to me why the parent argument does not work in this case.
Do you have any insight in this?