0

So I have a custom post title being written by a front-end (non-admin access) form using AFC fields. What I am after is a way to update the title when the post is updated from the front-end edit form.

Here's the working code to assign the post title from ACF fields in the functions.php:

function auto_title_insert( $value ) {
    if ( !$value ) :
        $value = $_POST['fields']['field_538626f57e84c'].' '.$_POST['fields']['field_538627ffeccb0'].' '.$_POST['fields']['field_53863a5c7502b'].' '.$_POST['fields']['fields[field_53a9bb09f82ba]'];
        return $value;
    endif;
    return $value;
}
add_filter( 'title_save_pre', 'auto_title_insert' );

The above code works great, however the title doesn't update when the custom post type is updated, it remains the first title assigned from the above code.

Any idea how to update the title when the post is updated?

  • 4
    Please don't roll my changes back. Your code is unreadable and you are using incorrect tags. Thank you – Pieter Goosen Nov 20 '14 at 19:08
1

You check if !$value before assigning the title from the fields, that test will be false once the post has a title. If you always want it to update, then remove the test.

function auto_title_insert() {
    return $_POST['fields']['field_538626f57e84c'].' '.$_POST['fields']['field_538627ffeccb0'].' '.$_POST['fields']['field_53863a5c7502b'].' '.$_POST['fields']['fields[field_53a9bb09f82ba]'];
}
add_filter( 'title_save_pre', 'auto_title_insert' );
  • Thanks for the response! It didn't work though :/ – I_LOVE_WP Nov 21 '14 at 2:16
1

I got it working - yay! :) It works with or without the test that Milo suggested to remove, but I left it out since it's not necessary. It is, however, necessary to have the $value in the function brackets. To use this you must use your own ACF field Id's of course.

// generate post title on submit
function auto_title_insert( $value ) {
  $value = $_POST['fields']['field_538626f57e84c'].' '.$_POST['fields']['field_538627ffeccb0'].' '.$_POST['fields']['field_53863a5c7502b'].' '.$_POST['fields']['fields[field_53a9bb09f82ba]'];
  return $value;
}
add_filter( 'title_save_pre', 'auto_title_insert' );

// update post title on update
function my_acf_update_value( $value, $post_id, $field ) {
    global $_POST;
    $new_title = $value;
    $new_slug = sanitize_title( $new_title );
    $my_post = array(
        'ID'         => $post_id,
        'post_title' => $new_title,
        'post_name'  => $new_slug
  );
  wp_update_post( $my_post );   
}
add_filter('acf/update_value', 'my_acf_update_value', 10, 3);

Oh yeah, one more thing - the following "post_title" specification has to be included in the AFC args array. I included it in both the original post creation form and the post edit form.

$args = array(
    'post_title'  => $_POST['acf-field-make'],
);
acf_form( $args );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.