I don’t think you need to use save_post
to save backend, as acf/save_post
also saves when updating the post in the backend. I think (95% sure).
Read the comments below in the code and see if this will help you create the custom post title you are after. Taking into account cars which only have 1 model, multiple models and no models. It’s a start anyway.
// run modifications when creating/updating the car post type
add_action('acf/save_post', 'acf_save_car', 20);
/**
* action to run modifications when creating/updating the car post type
* @param int $post_id
* @return void
*/
function acf_save_car($post_id) {
// get our current global post object data
global $post;
// check we are on post type cars else return early
if($post->post_type <> 'cars') return;
// if car post status is publish or draft
if($post->post_status == 'publish' || $post->post_status == 'draft') {
// create car object from current post object
$car = $post;
// get your car name custom field (acf way) and add to title array
$car_name = get_field('car_name', $post_id);
// get your model acf repeater field (not sure of field name for repeater)
$model_data = get_field('car_repeater', $post_id);
// set model span var as false incase no models exist
$model_span = false
// if model items exist in repeater
if($model_data) {
// create empty models array
$models = [];
// if only one model exists in repeater
if(count($model_data) === 1) {
// get the car model name and add it to our array
$models[] = $model_data[0]['car_model']
} else {
// get the first and last model data
$model_first = current($model_data);
$model_last = end($model_data);
// add this data to our model array
$models[] = $model_first['car_model'];
$models[] = $model_last['car_model'];
}
// create our model title segment with squiggly separator
$model_span = implode(' ~ ', $models);
}
// empty car post title array
$car_post_title = []
// add car title segments to car post title array if they exist
$car_name ? $car_post_title[] = $car_name : false;
$model_span ? $car_post_title[] = $model_span : false;
// temp remove the save car action
remove_action('acf/save_post', 'acf_save_car', 20);
// update our car object with new data
$car->post_title = implode(' - ', $car_post_title);
$car->post_name = sanitize_title($car->post_title);
// update the current post with updated car object
wp_update_post($car);
// re add the save car action
add_action('acf/save_post', 'acf_save_car', 20);
}
}