Home › Forums › General Issues › Create Category Tag/Term name from ACF Field
Hello.
I am building some front-end forms to create posts and add categories to specific custom taxonomies and I would like to be able to pass an ACF text field to create the category name and slug.
I’ve been successful in doing so with a post but not so much with a new category.
I have a custom post type called contacts
and I use the first and last name to update the title of the post with this:
add_action('acf/save_post', 'my_save_post', 20);
function my_save_post($post_id){
if( get_post_type($post_id) == 'contacts' ) {
// Get the data from a field
if( have_rows('contact_details') ):
while( have_rows('contact_details') ): the_row();
$first_name = get_sub_field('first_name', $post_id);
$last_name = get_sub_field('last_name', $post_id);
$title = $last_name . ', ' . $first_name;
$slug = $last_name . '-' . $first_name;
endwhile;
endif;
// Set the post data
$postdata = array(
'ID' => $post_id,
'post_title' => $title,
'post_type' => 'contacts',
'post_name' => $slug
);
// Remove the hook to avoid infinite loop. Please make sure that it has
// the same priority (20)
remove_action('acf/save_post', 'my_save_post', 20);
// Update the post
wp_update_post( $postdata );
// Add the hook back
add_action('acf/save_post', 'my_save_post', 20);
}
}
I want to do the same for a custom taxonomy within that post type called dealership
and instead have a simple acf text field dealership_name
pass that data to create the title and slug.
This is what I am trying to work with to make that happen but have a hit a wall.
add_action('acf/save_post', 'save_dealership_name', 20);
function save_dealership_name($post_id){
if( get_post_type($post_id) == 'contacts' ) {
$dealership_name = get_field('dealership_name', $post_id);
$last_name = get_sub_field('last_name', $post_id);
$title = $dealership_name;
$slug = $dealership_name;
endif;
// Set the post data
$postdata = array(
'ID' => $post_id,
'post_type' => 'contacts',
'term_name' => $title,
'term_slug' => $title
);
remove_action('acf/save_post', 'save_dealership_name', 20);
// Update the post
wp_update_post( $postdata );
// Add the hook back
add_action('acf/save_post', 'save_dealership_name', 20);
}
}
Any help would be greatly appreciated!
Forgive the bump!
Still struggling with this one and any help would be awesome!
You need to use wp_insert_term() to add the new term to the taxonomy. Before you add the new term I would check to see if it exists using get_term_by() on the new term slug. After you insert the new term or get the existing term then you need to wp_set_object_terms().
Thank you for the response.
Do you have links to previous discussions that could help me further? I can’t find anything that would help fit for my case and it only makes sense that someone else has needed to something similar.
I cannot find any examples of what you are looking for. This is not really something that people would try to do often because the taxonomy field can be set to allow adding terms and is how it would usually be done.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.