1

Is it possible to assign a custom post title name to the post ID upon submitting an ACF form? I'm trying to do this because I don't use the custom post title, instead I just want it to be the post ID. I'm using the "twentythirteen" template.

Here's what I have 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 problem is, the title doesn't change when the custom post type is updated

Here's what I have for when the post is updated:

function my_acf_update_value( $value, $post_id, $field ) {
    global $_POST;
    // vars
    $new_title = get_field('make', $post_id) . ' ' . $value;
    $new_slug = sanitize_title( $new_title );
    // update post
    // http://codex.wordpress.org/Function_Reference/wp_update_post
      $my_post = array(
      'ID'              => $post_id,
      'post_title'      => $new_title,
      'post_name'       => $new_slug
  );
// Update the post into the database
  wp_update_post( $my_post );   
}
add_filter('acf/update_value/make=make', 'my_acf_update_value', 10, 3);

The problem is I can only get it to work for one custom field instead of updating multiple fields

Here's what I have to make the title name the time stamp:

function auto_title_insert( $value ) {
  if ( !$value ) :
    $value = '['.get_the_date('d/m/Y @ H:i:s').']';
  endif;
  return $value;
}
add_filter( 'title_save_pre', 'auto_title_insert' );

That's ok, but I really want the post ID for organization

1
  • You are aware that you should sanitize stuff that you get from $_POST, right? I hope you don't need this link - codex.wordpress.org/…, and you are showing some early PoC, that is soon to be made safe :)
    – vlood
    Commented Dec 25, 2014 at 21:23

1 Answer 1

2

You could set it to the post ID when the post is first created via the default_title filter.

function wpd_default_title_filter( $post_title, $post ) {
    if( 'your_post_type' == $post->post_type ) {
        return $post->ID;
    }
    return $post_title;
}
add_filter( 'default_title', 'wpd_default_title_filter', 20, 2 );
2
  • That didn't work :/ I don't think it's possible to grab a post ID during the post creation. Thanks for the response!
    – I_LOVE_WP
    Commented Nov 20, 2014 at 18:50
  • @I_LOVE_WP sure it is, I've tested it. When you create a new post it immediately has an ID, so all the other stuff like custom fields and taxonomies have an ID to save data with. Make sure you change your_post_type to your actual post type slug in the example above.
    – Milo
    Commented Nov 20, 2014 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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