Skip Navigation

[Resolved] CRED form contains a field for state and county. i want to link to a taxonomy.

This thread is resolved. Here is a description of the problem and solution.

Problem:

I have a form which now has some required fields, such as name, state and county. I have a taxonomy called StatesCounties which is hierarchical, with a State and under it all of its Counties. But filling out the form does not set the taxonomy, or i thought it used to but not now. How to make sure if someone fills out this form, those taxonomies get included in its data?

Solution:

It needs custom codes, for example:

https://toolset.com/forums/topic/cred-form-contains-a-field-for-state-and-county-i-want-to-link-to-a-taxonomy/#post-1651189

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

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

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/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by edr-2 3 years, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#1651069

Tell us what you are trying to do? I have a form which now has some required fields, such as name, state and county. I have a taxonomy called StatesCounties which is hierarchical, with a State and under it all of its Counties. But filling out the form does not set the taxonomy, or i thought it used to but not now. How to make sure if someone fills out this form, those taxonomies get included in its data?

Is there any documentation that you are following? no

Is there a similar example that we can see? you can fill out the form at
hidden link , and then i could take a screen shot showing me edit the item
and show that the StatesCounties categories are empty for its data.
These terms are needed for mapping the user who filled out the form, on the website map by state and county.

What is the link to your site? hidden link

#1651189

Hello,

I assume we are talking about those two fields of the URL you mentioned above:
- State
- County

They seem to be custom fields, if you want to use the custom field values to setup the taxonomy "StatesCounties " value, it needs custom codes, for example:
1) When user submit the form, use action hook "cred_save_data" to trigger a PHP function:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

2) In this PHP function, get the those custom field values:
- State
- County
https://developer.wordpress.org/reference/functions/get_post_meta/

Then use above values to update the taxonomy "StatesCounties " value:
https://developer.wordpress.org/reference/functions/wp_set_object_terms/

#1652173
dashboard-type-cfs-taxonomies.png
post-type-attorney.png
statescounties.png

just to be sure we are on the same page before i attempt anything, see these snapshots
please let me know if your answers are still relevant to my situation.
thanks!

#1652293
form-id.png

i found some code that i am working with to try to write to the StatesCounties taxonomy for the user who filled out the CRED form.
At the toolset dashboard when i edit the form i am using the part of the url for the form id ( see attached) is this the correct id?

add_action('cred_save_data', 'category_save_data_action',20,2);

function category_save_data_action($post_id, $form_data){

// if a specific form

if ($form_data['id']==699) {

$parent_post_id = $_GET['parent_assignment_id'];

$parent_terms = wp_get_object_terms( $parent_post_id, 'lab-categories', array('ids') );

if(!empty($parent_terms)) {

$parent_term_ids = array();

foreach($parent_terms as $parent_term) {

if($parent_term->term_id != '') {

$parent_term_ids[] = $parent_term->term_id;

}
}

if(count($parent_term_ids) > 0) {

wp_set_object_terms( $post_id, $parent_term_ids , 'lab-categories' );

}
}

}

}

#1652445

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.