0

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.

5
  • 1
    Have you already checked the docs? You could also do var_dump( get_post_meta( $post_id ) ) to check the metadata list.
    – Sally CJ
    Commented Jul 15, 2020 at 8:25
  • @sally yes, but I couldn't get it to work.
    – L. core
    Commented Jul 15, 2020 at 18:07
  • @SallyCJ I hope I understood you correctly, you mean for the repeater, right? I edited the question.
    – L. core
    Commented Jul 16, 2020 at 15:11
  • No, not really, I just wanted to check the get_post_meta()'s output beforehand. But anyway, I've removed that comment.
    – Sally CJ
    Commented Jul 18, 2020 at 8:23
  • 1
    @SallyCJ Sorry, and thanks anyway. I appreciate giving me your time.
    – L. core
    Commented Jul 18, 2020 at 9:54

2 Answers 2

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

}
1

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)
2
  • 1
    make sure you got WP DEBUG on as you may get some warnings if fields have not been set. You should ideally check if they exist first before getting array items by key number.
    – joshmoto
    Commented Jul 18, 2020 at 9:46
  • @joshmoto I assigned the fields as "required", so they must be filled, but I'll do as you said just in case. Thank you for your help.
    – L. core
    Commented Jul 18, 2020 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.