Passer la navigation

[Résolu] Use toolset_forms_frontend_flow_form_start to filter Taxonomy Field of CRED

This support ticket is created Il y a 4 years, 10 months. 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 -

Fuseau horaire du supporter : Asia/Karachi (GMT+05:00)

Ce sujet contient 3 réponses, a 2 voix.

Dernière mise à jour par smileBeda Il y a 4 years, 9 months.

Assisté par: Waqar.

Auteur
Publications
#2080247

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?

#2081187

Hi Beda,

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

I can confirm seeing the same results as your tests and couldn't make the taxonomy filter based on a parent term.

I've shared this with the concerned team for further review, to see if they have some more pointers/suggestions and will keep you updated.

regards,
Waqar

#2082643

Hi Beda,

Thank you for waiting and I've heard back from the development team.

This limitation lies in the fact that the current mechanism to render taxonomy term fields in the form expects the term hierarchy to be maintained and the top level of that tree needs to be made of grand-parent terms (i.e. terms with no parent). This is why setting the parent to '0' works but setting any other parent term doesn't.

As we currently don't offer any official API or settings for this type of filtering, it has been noted as a feature request.

For now, the only workaround that I can think of showing the selective terms is using the term meta filtering (instead of the parent level filtering), as suggested by Jamal in his thread.

regards,
Waqar

#2082655

Ok thank you for the update Waqar.

I will use a Custom Form for this because it is required to filter by parents, thus I cannot really filter by custom fields (without a lot of additional work for the client)

Thanks...