1

I have 3 custom fields as shown below. I want to use them to generate the title when I save/publish... etc

The code works if I insert the post id as a number but not like this...

function set_post_title( $data , $postarr ) {

    $title = "";
    $post_id = $postarr['ID'];     //NOT WORKING

    $last_name = get_post_meta($post_id, 'last-name', true);
    $middle_name = get_post_meta($post_id, 'middle-name', true);
    $first_name = get_post_meta($post_id, 'first-name', true);

//        $last_name = $data['last-name'];
//        $middle_name = $data['middle-name'];
//        $first_name = $data['first-name'];
//      also didn't work...

    if ( $last_name != "" ) {
        $title .= $last_name . ", ";
    }

    if ( !empty( $first_name ) ) {
        $title .= $first_name;
    }

    if ( !empty( $middle_name ) ) {
        $title .= " " . $middle_name;
    }

    $data['post_title'] = $title."aa".$post_id;

return $data;
}
add_filter( 'wp_insert_post_data' , 'set_post_title' );

If it would be easier I save my field values in another function using:

    update_post_meta( $post_id, 'first-name', $_POST['first-name'] );

Is there something similar to update the title?

This has been driving me mad :)

1

From the codex page

You must pass 2 for the $accepted_args variable in add_filter() if you want to access $postarr.

So use this:

add_filter( 'wp_insert_post_data', 'set_post_title', 99, 2 );
1
  • Thank you so much, I've been going in circles. I actually removed that for some reason, thought it was optional... :)
    – Oliver
    Jun 8 '15 at 17:36

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.