Home › Forums › General Issues › Populate acf_form title with taxonomy term value
Hi,
I have a basic front end form set up with a selected field group that saves it’s data into a custom post type, nothing too complicated. However, I’m having an issue generating a post title from a taxonomy term.
Basically, the user can select one term value from a dropdown select list on the form (as well as fill out other fields), then submit the form. I want the post title to reflect the string value of the term, but instead it’s just returning the term ID.
Here’s my code:
functions.php
function my_save_post( $post_id ) {
if ($post_type != 'points') {
return;
}
$taxonomy = $_POST['acf']['field_59940ffa4a644'];
$title = $taxonomy;
$new_post = array(
'ID' => $post_id,
'post_status' => 'publish',
'post_title' => $title,
'post_type' => 'points'
);
remove_action('acf/save_post', 'my_save_post', 20);
wp_update_post( $new_post );
add_action('acf/save_post', 'my_save_post', 20);
}
add_action('acf/save_post', 'my_save_post', 20);
page-template.php
acf_form(
array(
'id' => 'form-points',
'post_id' => 'new_post',
'field_groups' => array(126),
'html_submit_button' => '<input type="submit" class="btn btn-samurai" value="%s" />',
'submit_value' => 'Submit Points',
'updated_message' => 'Points Created Successfully'
)
);
To recap… the form works, but it saves the title of the post as an integer value (the term ID perhaps?), rather than the actual term string value.
Any help would be awesome, thanks in advance!
Hey shanekins,
You seem to be pretty close with this.. assuming that the dropdown you have created is returning the term ID, all you need to do is use the built in WordPress function ‘get_term’ to get the taxonomy’s name:
function my_save_post( $post_id ) {
// Pull the Term ID from the form:
$term_id = $_POST['acf']['field_59940ffa4a644'];
// Now get the WordPress term Object:
$term = get_term( $term_id );
// Check to see that a term is returned successfully
if ( ! empty( $term && ! is_wp_error( $term ) ) )
{
// Now you can get the term name for your title
$title = $term->name;
$new_post = array(
'ID' => $post_id,
'post_status' => 'publish',
'post_title' => $title,
'post_type' => 'points'
);
remove_action('acf/save_post', 'my_save_post', 20);
wp_update_post( $new_post );
add_action('acf/save_post', 'my_save_post', 20);
}
}
add_action('acf/save_post', 'my_save_post', 20);
The topic ‘Populate acf_form title with taxonomy term value’ is closed to new replies.
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.