plugins – Auto-saving post title based on custom field – for custom post type

Question

I’ve set up a CPT via a plugin, including custom fields for first and last name, so I’d like to automatically make the post title “First Name + ‘ ‘ + Last Name”.

This post almost got me there:

How To Set Custom Post Type Title Without Supports

But my version (code below) creates a title called “Array Array”.

add_filter( 'save_post_practitioners', 'hexagon_practitioner_set_title', 10, 3 );
function hexagon_practitioner_set_title ( $post_id, $post, $update ){
    //This temporarily removes filter to prevent infinite loops
    remove_filter( 'save_post_practitioners', __FUNCTION__ );

    //get first and last name meta
    $first = get_metadata( 'first_name', $post_id ); //meta for first name
    $last = get_metadata( 'last_name', $post_id );   //meta for last name

    $title = $first . ' ' . $last;

    //update title
    wp_update_post( array( 'ID'=>$post_id, 'post_title'=>$title ) );

    //redo filter
    add_filter( 'save_post_practitioners', __FUNCTION__, 10, 3 );
}

What am I getting wrong?

Thanks!

0
Adam Abrams 1 month 2023-02-14T16:14:38-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse