How to retrieve the values of a sub-field in the first and last row of an (ACF) repeater inside function?

I’m using a function that changes the front-end and back-end post title for a custom post type according to what you put in certain custom fields.

    //Save ACF fields as post_content for back-end
add_action('save_post', 'change_title_cars');

function change_title_cars($post_id) {
    global $_POST;
    if('cars'== get_post_type()){

      $car_name = get_post_meta($post_id,'car_name',true);
      $first_car_model = ????????????;
      $last_car_model = ????????????;

      $my_post = array();
            $my_post['ID'] = $post_id;
            $my_post['post_title'] = $car_name . ' - ' . $first_car_model . ' ~ ' . $last_car_model;

remove_action('save_post', 'change_title_cars');
                    wp_update_post( $my_post );
add_action('save_post', 'change_title_cars');
    }   
   }


//Save ACF fields as post_content for front-end
add_action('acf/save_post', 'change_title_frontend_cars');

function change_title_frontend_cars($post_id) {
    global $_POST;
    if('cars'== get_post_type()){

      $car_name = get_post_meta($post_id,'car_name',true);
      $first_car_model = ????????????;
      $last_car_model = ????????????;

      $my_post = array();
            $my_post['ID'] = $post_id;
            $my_post['post_title'] = $car_name . ' - ' . $first_car_model . ' ~ ' . $last_car_model;

    remove_action('acf/save_post', 'change_title_frontend_cars');
                    wp_update_post( $my_post );
    add_action('acf/save_post', 'change_title_frontend_cars');
        }
    }

I’m using a repeater which has a sub-field named “car_model”. I’m trying to have the title changed into something like this:

(car_name) – (first “car_model” in the repeater) ~ (last “car_model” in the repeater)

but I couldn’t get values of the repeater sub-fields inside the function.

Note: the repeater has random number of rows.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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);
    
    }

}

Method 2

All I needed in my code for it to work is this:

    //get the repeater
    $car_repeater = get_field('repeater_name',$post_id); 
    
    
    $first_row = $car_repeater[0]; //put [0] to get the first row of the repeater
    $first_car_model = $first_row['car_model']; //put [car_model] to get this field value in the specified row (first row)
    
    
    $last_row = end($car_repeater); //use end() to get the last row of the repeater
    $last_car_model = $last_row['car_model']; //put [car_model] to get this field value in the specified row (last row)


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x