Home › Toolset Professional Support › [Resolved] Attach multiple values from address and select custom fields to taxonomy
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.
Our next available supporter will start replying to tickets in about 6.78 hours from now. Thank you for your understanding.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10: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/Kolkata (GMT+05:30)
This topic contains 13 replies, has 2 voices.
Last updated by JEN 2 years, 9 months ago.
Assisted by: Minesh.
Tell us what you are trying to do?
I am attempting to set multiple taxonomy terms with values from cred post form fields. This is already working well with attaching one 'state' field from an address field but I'm having trouble adapting for multiple values. In addition to the address field there are 10 select fields that also set a 'state' which I also need to set taxonomy terms for on both create post and edit post cred forms.
Here is the working code for attaching a single term which Waqar provided, I adapted just to customize the function names:
// add state to category add_action( 'save_post', 'ied_state_save_post', 10,3 ); function ied_state_save_post( $post_id, $post, $update ) { // exit if not the target post type if ( ('listing' !== $post->post_type) OR ($post->post_status == 'auto-draft') OR (empty($_POST)) ) { return; } // current post ID $post_id = $post->ID; // target taxonomy slug $taxonomy_slug = 'state'; // $_POST['wpcf-state'] value exists, it means that post is being added using the front-end form if(!empty($_POST['wpcf-state'])) { // get the term's name from the form's state field $term_name = $_POST['wpcf-state']; // call the function that processes the taxonomy attachment ied_new_state_term($post_id, $term_name, $taxonomy_slug); } // $_POST['wpcf']['state'] value exists, it means that post is being added using the back-end post edit screen if(!empty($_POST['wpcf']['state'])) { // get the term's name from the state field $term_name = $_POST['wpcf']['state']; // call the function that processes the taxonomy attachment ied_new_state_term($post_id, $term_name, $taxonomy_slug); } } // function that processes the taxonomy attachment function ied_new_state_term($post_id, $term_name, $taxonomy_slug) { // check if the term already exists $term = term_exists( $term_name, $taxonomy_slug ); // if does exist if ( $term !== 0 && $term !== null ) { // attach it to the current post wp_set_post_terms( $post_id, $term['term_id'], $taxonomy_slug ); } // if doesn't exist else { // first add it as a new term in the taxonomy $inset_new_term = wp_insert_term($term_name, $taxonomy_slug); if ( ! is_wp_error( $inset_new_term ) ) { // and then attach this new term to the current post $term_id = $inset_new_term['term_id']; wp_set_post_terms( $post_id, $term_id, $taxonomy_slug ); } } }
Is there any documentation that you are following?
Here is the post it was provided in:
https://toolset.com/forums/topic/split-extracting-address-data-from-maps-field-and-saving-as-taxonomy-term/
Here are the additional fields I'm wanting to attach:
$_POST['wpcf-license-state'] $_POST['wpcf-2nd-license-state'] $_POST['wpcf-3rd-license-state'] $_POST['wpcf-4th-license-state'] $_POST['wpcf-5th-license-state'] $_POST['wpcf-6th-license-state'] $_POST['wpcf-7th-license-state'] $_POST['wpcf-8th-license-state'] $_POST['wpcf-9th-license-state'] $_POST['wpcf-10th-license-state']
Ironically I've learned a lot about PHP working on this project but I'm still learning.
Thank you!
Hello. Thank you for contacting the Toolset support.
Can you please share that you already have the field $_POST['wpcf-state'] and the other field that you shared as follows are the different addon fields, means you are having $_POST['wpcf-state'] field plus the following fields?
$_POST['wpcf-license-state'] $_POST['wpcf-2nd-license-state'] $_POST['wpcf-3rd-license-state'] $_POST['wpcf-4th-license-state'] $_POST['wpcf-5th-license-state'] $_POST['wpcf-6th-license-state'] $_POST['wpcf-7th-license-state'] $_POST['wpcf-8th-license-state'] $_POST['wpcf-9th-license-state'] $_POST['wpcf-10th-license-state']
That's correct, I have all the fields listed above plus another field wpcf--state which is populated by jQuery in the post form settings from the Google map field.
I can give access to a staging site if it helps.
Please take backup of your existing code and what if you try to use the following code:
add_action( 'save_post', 'ied_state_save_post', 10,3 ); function ied_state_save_post( $post_id, $post, $update ) { // exit if not the target post type if ( ('listing' !== $post->post_type) OR ($post->post_status == 'auto-draft') OR (empty($_POST)) ) { return; } // current post ID $post_id = $post->ID; // target taxonomy slug $taxonomy_slug = 'state'; // $_POST['wpcf-state'] value exists, it means that post is being added using the front-end form if(!empty($_POST['wpcf-state'])) { // get the term's name from the form's state field $term_name = $_POST['wpcf-state']; // call the function that processes the taxonomy attachment ied_new_state_term($post_id, $term_name, $taxonomy_slug); } // $_POST['wpcf']['state'] value exists, it means that post is being added using the back-end post edit screen if(!empty($_POST['wpcf']['state'])) { // get the term's name from the state field $term_name = $_POST['wpcf']['state']; // call the function that processes the taxonomy attachment ied_new_state_term($post_id, $term_name, $taxonomy_slug); } //// $_POST['wpcf-license-state'] if(!empty($_POST['wpcf-license-state'])) { // get the term's name from the form's state field $term_name = $_POST['wpcf-license-state']; // call the function that processes the taxonomy attachment ied_new_state_term($post_id, $term_name, $taxonomy_slug); } if(!empty($_POST['wpcf']['license-state'])) { // get the term's name from the state field $term_name = $_POST['wpcf']['license-state']; // call the function that processes the taxonomy attachment ied_new_state_term($post_id, $term_name, $taxonomy_slug); } //// $_POST['wpcf-2nd-license-state'] if(!empty($_POST['wpcf-2nd-license-state'])) { // get the term's name from the form's state field $term_name = $_POST['wpcf-2nd-license-state']; // call the function that processes the taxonomy attachment ied_new_state_term($post_id, $term_name, $taxonomy_slug); } if(!empty($_POST['wpcf']['2nd-license-state'])) { // get the term's name from the state field $term_name = $_POST['wpcf']['2nd-license-state']; // call the function that processes the taxonomy attachment ied_new_state_term($post_id, $term_name, $taxonomy_slug); } //// $_POST['wpcf-3rd-license-state'] if(!empty($_POST['wpcf-3rd-license-state'])) { // get the term's name from the form's state field $term_name = $_POST['wpcf-3rd-license-state']; // call the function that processes the taxonomy attachment ied_new_state_term($post_id, $term_name, $taxonomy_slug); } if(!empty($_POST['wpcf']['3rd-license-state'])) { // get the term's name from the state field $term_name = $_POST['wpcf']['3rd-license-state']; // call the function that processes the taxonomy attachment ied_new_state_term($post_id, $term_name, $taxonomy_slug); } //// $_POST['wpcf-4th-license-state'] if(!empty($_POST['wpcf-4th-license-state'])) { // get the term's name from the form's state field $term_name = $_POST['wpcf-4th-license-state']; // call the function that processes the taxonomy attachment ied_new_state_term($post_id, $term_name, $taxonomy_slug); } if(!empty($_POST['wpcf']['4th-license-state'])) { // get the term's name from the state field $term_name = $_POST['wpcf']['4th-license-state']; // call the function that processes the taxonomy attachment ied_new_state_term($post_id, $term_name, $taxonomy_slug); } //// $_POST['wpcf-5th-license-state'] if(!empty($_POST['wpcf-5th-license-state'])) { // get the term's name from the form's state field $term_name = $_POST['wpcf-5th-license-state']; // call the function that processes the taxonomy attachment ied_new_state_term($post_id, $term_name, $taxonomy_slug); } if(!empty($_POST['wpcf']['5th-license-state'])) { // get the term's name from the state field $term_name = $_POST['wpcf']['5th-license-state']; // call the function that processes the taxonomy attachment ied_new_state_term($post_id, $term_name, $taxonomy_slug); } //// $_POST['wpcf-6th-license-state'] if(!empty($_POST['wpcf-6th-license-state'])) { // get the term's name from the form's state field $term_name = $_POST['wpcf-6th-license-state']; // call the function that processes the taxonomy attachment ied_new_state_term($post_id, $term_name, $taxonomy_slug); } if(!empty($_POST['wpcf']['6th-license-state'])) { // get the term's name from the state field $term_name = $_POST['wpcf']['6th-license-state']; // call the function that processes the taxonomy attachment ied_new_state_term($post_id, $term_name, $taxonomy_slug); } //// $_POST['wpcf-7th-license-state'] if(!empty($_POST['wpcf-7th-license-state'])) { // get the term's name from the form's state field $term_name = $_POST['wpcf-7th-license-state']; // call the function that processes the taxonomy attachment ied_new_state_term($post_id, $term_name, $taxonomy_slug); } if(!empty($_POST['wpcf']['7th-license-state'])) { // get the term's name from the state field $term_name = $_POST['wpcf']['7th-license-state']; // call the function that processes the taxonomy attachment ied_new_state_term($post_id, $term_name, $taxonomy_slug); } //// $_POST['wpcf-8th-license-state'] if(!empty($_POST['wpcf-8th-license-state'])) { // get the term's name from the form's state field $term_name = $_POST['wpcf-8th-license-state']; // call the function that processes the taxonomy attachment ied_new_state_term($post_id, $term_name, $taxonomy_slug); } if(!empty($_POST['wpcf']['8th-license-state'])) { // get the term's name from the state field $term_name = $_POST['wpcf']['8th-license-state']; // call the function that processes the taxonomy attachment ied_new_state_term($post_id, $term_name, $taxonomy_slug); } //// $_POST['wpcf-9th-license-state'] if(!empty($_POST['wpcf-9th-license-state'])) { // get the term's name from the form's state field $term_name = $_POST['wpcf-9th-license-state']; // call the function that processes the taxonomy attachment ied_new_state_term($post_id, $term_name, $taxonomy_slug); } if(!empty($_POST['wpcf']['9th-license-state'])) { // get the term's name from the state field $term_name = $_POST['wpcf']['9th-license-state']; // call the function that processes the taxonomy attachment ied_new_state_term($post_id, $term_name, $taxonomy_slug); } //// $_POST['wpcf-10th-license-state'] if(!empty($_POST['wpcf-10th-license-state'])) { // get the term's name from the form's state field $term_name = $_POST['wpcf-10th-license-state']; // call the function that processes the taxonomy attachment ied_new_state_term($post_id, $term_name, $taxonomy_slug); } if(!empty($_POST['wpcf']['10th-license-state'])) { // get the term's name from the state field $term_name = $_POST['wpcf']['10th-license-state']; // call the function that processes the taxonomy attachment ied_new_state_term($post_id, $term_name, $taxonomy_slug); } } // function that processes the taxonomy attachment function ied_new_state_term($post_id, $term_name, $taxonomy_slug) { // check if the term already exists $term = term_exists( $term_name, $taxonomy_slug ); // if does exist if ( $term !== 0 && $term !== null ) { // attach it to the current post wp_set_post_terms( $post_id, $term['term_id'], $taxonomy_slug ); } // if doesn't exist else { // first add it as a new term in the taxonomy $inset_new_term = wp_insert_term($term_name, $taxonomy_slug); if ( ! is_wp_error( $inset_new_term ) ) { // and then attach this new term to the current post $term_id = $inset_new_term['term_id']; wp_set_post_terms( $post_id, $term_id, $taxonomy_slug ); } } }
Hi Mines! I tried out the code in the email I received. I entered an address, a license-expiration and a 2nd-license-expiration but only the third of those, 2nd-license-expiration, term was added.
Can you please share problem URL and access details and tell me where exactly you've added the code I shared.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
I'll require FTP access details so that I can access the code file: /wp-content/themes/generatepress_ied/lib/toolset.php
Also, Can you please share frontend user access details (user/pwd) for which I can see both Add and Edit listing.
I'm not sure why Waqar used save_post hook as if you are using Toolset Form the correct hook needs to be used is "cred_save_data" but as you claim its working - let me see if I'm able to manage it with the code solution he shared.
I have set the next reply to private which means only you and I have access to it.
REMOVED this was supposed to be private
That last replay was not marked private.
Please resend the private link if you did not already receive the login details so I can add them securely. Thanks!
On Account page: hidden link
- I just see the "Create Listing" link. Can you please share user access details for which I can see both Create and Edit lising.
In addition to that, I could not able to login to FTP using the SFTP access details you shared. Please share correct SFTP access details.: Host, User, Password and port (if any).
I have set the next reply to private which means only you and I have access to it.
I already made reply private I'm not sure why its not working at your end.
Again, I'm making the next reply as private.
Hi Minesh,
I have tested the SFTP login and it works, not sure why it is not for you but I just tested it again and it works.
EDIT LISTING does not show on ACCOUNT until after you CREATE LISTING. Once there is a listing associated with your user, the Edit Listing link will appear.
***REMOVED THIS WAS SUPPOSED TO BE PRIVATE. I AM FOLLOWING THE LINKS IN THE EMAILS SENT. NOT SURE WHY MY REPLES ARE NOT PRIVATE.**
Thank you!
Hi again,
I'm experiencing another problem.
When I try to add a new checkbox field to my Listings post type fields it deletes a whole bunch of fields.
Once I add the checkbox everything past the 6th license disappears, there are about 30 fields after. I have tried this now 3x with the same result. Everything after 6th License State is gone after I save.
To test I completely disabled all the customizations in my toolset.php file but it still happens. It almost seems like a limit of Toolset.
Is it because it's a staging site? To test I deleted a bunch of fields and then I was able to add a couple new ones. Perhaps Toolset limits what you can do when the site is not registered? If so, how to register a staging site.
Figured this out myself.
The code provided did not work as it was calling wp_set_post_terms multiple times. Each time it's called the terms are overwritten. The terms needed to be put into an array and then wp_set_post_terms called only once.