1 year ago
#379026
Christi Yarema
Advanced Custom Field Front End Form not producing the right Slug/Permalink
I've grabbed this code from the Advanced Custom Fields website to make an employee directory with a front-end ACF form. But I can't get it to produce a permalink that is the title employee name, which is the title of the post. It just produces a random number. Permalinks are set to Post Name Here is the code in my template file:
<?php while ( have_posts() ) : the_post(); ?>
<?php
acf_form(array(
'post_id' => 'new_post',
'post_title' => wp_strip_all_tags($_POST['acf']['field_624c65c9cc5da']), // Post Title ACF field key
'return' => '%post_url%',
'new_post' => array(
'post_type' => 'staff',
'post_status' => 'publish'
)
));
?>
<?php endwhile; ?>
And this is the code in my functions PHP file:
add_action('acf/save_post', 'my_save_post', 20);
function my_save_post($post_id){
// Get the data from a field
$new_title = get_field('staff_name', $post_id);
// Set the post data
$new_post = array(
'ID' => $post_id,
'post_title' => $new_title,
);
// 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( $new_post );
// Add the hook back
add_action('acf/save_post', 'my_save_post', 20);
}
wordpress
advanced-custom-fields
0 Answers
Your Answer