Skip Navigation

[Resolved] Split: Extracting Address Data from Maps Field and saving as taxonomy term

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

Problem:

The customer asked if it is possible to attach the "state" component from an address type custom field as a taxonomy term.

Solution:

Shared a code example, involving the "save_post" hook.
( ref: https://toolset.com/forums/topic/split-extracting-address-data-from-maps-field-and-saving-as-taxonomy-term/#post-2255333 )

Relevant Documentation:

n/a

This support ticket is created 2 years, 3 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/Karachi (GMT+05:00)

Author
Posts
#2254505

JEN

Hi again Waqar, I realize this thread is closed but figured best to ask this follow up question here first so the info stays together. Hopefully you see this.

Would it be possible to write the state data to a custom taxonomy instead of a types field? I have a Hierarchical taxonomy already created for it with slug "state" but not sure how to modify the code to write to that instead.

Reason is so a list of links can be displayed with the state only showing if there are results which seems easiest to do with a category structure.

Thank you!

#2254509

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thanks for asking and since the other ticket was resolved, I've created a new ticket for this.

Achieving this would be more challenging in the admin area post edit screen, compared to the front-end forms.

I'll need to perform some tests on my website and will share my findings with you, accordingly.

regards,
Waqar

#2254513

JEN

Hi Waqar,

It's fine for this to be for the front end form only. Admins would rarely need to edit this and can be trained to use the regular category selector.

Thank you!

Jen

#2255333

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for waiting and on my test website, I was able to make this work for the front-end forms as well as the back-end post edit screen.

Note: Please make sure to keep the "state" custom field as discussed in the previous ticket, because it is easier to populate the state value in it, using the script. The value from that field is then used in the custom code for the processing of the taxonomy term assignment.

The following function executes when the post is saved (from front-end and back-end) and attaches the term with the same name in the taxonomy named "State":


add_action( 'save_post', 'custom_process_state_save_post', 10,3 );
function custom_process_state_save_post( $post_id, $post, $update ) {
	// exit if not the target post type
	if ( ('post' !== $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
		custom_process_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
		custom_process_new_state_term($post_id, $term_name, $taxonomy_slug);
	}
}

// function that processes the taxonomy attachment
function custom_process_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 );
		}
	}
}

Note: in this example, the target post type which includes this custom field and taxonomy is "post", taxonomy slug is "state" and the custom field slug is "state". Be sure to update these values, as per your website.

The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

#2255675

JEN

Waqar, you are the man!

This works perfectly. I updated to my post type and working on front end post form and admin edit form.

I am almost there with this site. I may need to get help with a couple things that are still left but will have a go myself first. Wish I could just hire *you* 🙂

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