get the title of a relationship field

Question

Im trying to set a custom post title with a custom field instead of typing the title im setting it with a custom field it works good if I use a type text custom field but once I use a relationship field Im getting nothing I guess because that field return a array, my relationship field is “route_affected_by_this_alert”

// Auto fill Title and Slug for CPT's - 'companies', 'contacts', 'properties'

function lh_acf_save_post( $post_id ) {

// Don't do this on the ACF post type
// if ( get_post_type( $post_id ) == 'project' ) {
//     return;
// }

$new_title = '';
$new_slug = '';

// Get title from 'companies' CPT acf field 'company_name'
if ( get_post_type( $post_id ) == 'alerts' ) {
    $new_title =  get_field( 'route_affected_by_this_alert', $post_id );
    $new_slug = sanitize_title( $new_title );
}

// Prevent iInfinite looping...
remove_action( 'acf/save_post', 'lh_acf_save_post' );

// Grab post data
$post = array(
    'ID'            => $post_id,
    'post_title'    => $new_title,
    'post_name'     => $new_slug,
);

// Update the Post
wp_update_post( $post );

// Continue save action
add_action( 'acf/save_post', 'lh_save_post' );

// Set the return URL in case of 'new' post
$_POST['return'] = add_query_arg( 'updated', 'true', get_permalink( $post_id ) );

}
add_action( ‘acf/save_post’, ‘lh_acf_save_post’, 10, 1 );

0
kelvin ramirez 7 months 0 Answers 84 views 0

Leave an answer