Skip Navigation

[Resolved] How to display only certain taxonomy terms on CRED form?

This support ticket is created 3 years, 8 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.

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: Africa/Casablanca (GMT+01:00)

This topic contains 15 replies, has 2 voices.

Last updated by stevenT-5 3 years, 8 months ago.

Assisted by: Jamal.

Author
Posts
#1967695

Tell us what you are trying to do?

Hi, I'm creating a frontend submission form to create new posts. On this form, the users get to select subject categories. My problem is that if I display the taxonomy field on the form, it shows ALL the terms I have for the web site. However, I only want to display and restrict only certain categories to frontend user to choose from. How can I achieve this setup?

Thank you.
Steven

#1968141

Hello and thank you for contacting the Toolset support.

This is not possible out of the box. But, it can be implemented using some custom code. Either on the get_terms default hook from WordPress. Or using the wpt_field_options hook from Toolset
https://toolset.com/documentation/programmer-reference/types-api-filters/#wpt_field_options

Check some example of using the filter on the following replies:
- https://toolset.com/forums/topic/dynamic-dropdown-values-added-using-wpt_field_options-not-working-as-filter/#post-597683
- https://toolset.com/forums/topic/dynamic-select-options-for-cred-user-form-fields/#post-624490
- https://toolset.com/forums/topic/create-dynamic-dropdown-of-terms-from-custom-taxonomy-on-cred-user-forms/#post-865079

#1968647

Thanks for the response, Jamal.

The techniques you reference above while maybe possible solutions, they don't apply to my case exactly. So I would need to experiment with the custom code. One, PHP is not my territory. Two, these sound more like workaround solutions than long term and stable setups. For now, I'd need to rethink my setup.

It would be nice if Tootset provides a way to manually select and limit with taxonomy terms to be available for the relevant form. Do you think this is something I can put in as a feature request? If so, where I can submit it?

#1968829

We currently take feature requests from the support forum. I escalated this request to our 2nd Tier for an initial evaluation. I'll get back to you as soon as he gets back to me.

In the meantime, please let me know if you need assistance with the custom code that will help you achieve what you want.

#1969357

Jamal, that would be great if you can help submit my request for a new feature. Thank you.

As for custom code, I'm not sure that's the way to go for my case. The site I'm working on keeps evolving including taxonomy terms. So I would't want having to edit the code everything the client wants to add new terms or edit existing ones. I feel a built in tool would be best so we can update the form quickly. I would need to come up with a different approach so that the form does not need to use taxonomy fields for now.

/ Steven

#1969493

Thank you, Steven,

I have already escalated the feature request to our 2nd Tier for evaluation. It can, then, be escalated to the developers. And if it is accepted by the development team, I am pretty sure that will take some time(a few months) before it lands in an upcoming release.

I understand that you want to allow your client to customize the terms of the fields using a user interface instead of a custom code. So, I'll suggest that you add a custom field(checkbox) to the taxonomy so the client can check the terms that he wants to use in the form. Then we can come up with a custom code that will display only the checked terms inside the form. Sounds good?

#1970309

Thanks for the offer and willing to collaborate, Jamal.

I see what you're suggesting. The setup I'm working with is that the site already has a good couple dozens of existing taxonomy terms used by lots of posts. We now want to create front-end form to allow public users to submit posts where they get to categorize the posts based on our existing terms we make available to them. So this is requirement one - we need to work with existing terms and not create new ones just for the form. The tricky part comes in that there are existing terms we don't want available to users, thus we want to hide certain terms from showing up on the form. Then requirement two would be to be able to easily display certain existing terms on the form without having to update the custom code every time there are changes on the terms.

Do you think we can pull this off?

Thank you.

#1970779

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.

#1971177

Thanks for your work on this, Jamal.

I checked out the what you have, and it looked pretty good. There's one little thing of requirement from client I haven't explained, is that on the final form you have with the dropdown menu to select the filtered categories, users would be able to do multiple select on the categories. So instead of a dropdown menu, can we have the categories as checkboxes?

Steven

#1972047

Hello Steven,

Yes, we can have checkboxes instead of a dropdown, it is configurable at the form, check this screenshot hidden link

And check this screenshot for the results hidden link
hidden link

#1979159

Please keep the the sandbox site and reference links above for a few more days as I still need a bit more time to implement the setup and test it out.

thank you

#1979519

Hello Steven,

The feature request has been escalated to the developers, it is not yet added to any board.

The sandbox site is configured to expire after 7 days from the last administrator login. And we have plans to drop using it from the current provider by the end of the month.
For that reason, I built an All In One Migration export and made it available for upload at the following link:
hidden link

I am adding below the whole custom code used in this solution:

// 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;
}

// 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);

// 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);

Check this screenshot hidden link

#1981491
Screen Shot 2021-03-10 at 3.04.01 PM.png

Hello Jamal,

I have set things up according to what you have. The two variables in the custom scripts you have are form ID and term filed slug which I have applied accordingly as they are not the same for my case. I test the form and the terms fields are not showing up as expected. When I turn off the custom code snippet in Toolset settings, then ALL category terms show up. So the scripts are not running correctly in my setup. I figure the best way to trouble shoot is for you to access the staging site and have a look first hand and let me know what discrepancies you may find. Below is access info. Please keep this post private as it contains sensitive info.

This is the frontend form in discussion:
hidden link

If you have a look at the attached screenshot, I indicate where the category terms supposed to show up.

Looking forward to your finding.
Thank you.

#1983509

Would you allow me temporary access to check this further? Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

#1984711

Hello Steven,

Everything was set correctly, you just forgot to check the terms to use. Once I checked one term to be used it appeared in the form. Here the term is checked hidden link and here it appears hidden link

Go to each term you want to be available in the form and enable the custom field on it. Does it make sense?