Support

Account

Home Forums ACF PRO Delete WP Title and use ACF Title Reply To: Delete WP Title and use ACF Title

  • I have more than one custom field, so code looks like this:

    // Get title from acf into post title
    add_action('acf/save_post', 'upd_cust_post_title', 20); // fires after ACF
    function upd_cust_post_title($post_id) {
      $post_type = get_post_type($post_id);
      if ($post_type != 'house') {
        return;
      }
      $post_title = get_field('name', $post_id);
      $post_name = sanitize_title($post_title);
      $post = array(
        'ID' => $post_id,
        'post_name' => $post_name,
        'post_title' => $post_title
      );
      wp_update_post($post);
    }
    
    add_action('acf/save_post', 'upd_car_post_title', 20); // fires after ACF
    
    function upd_ann_post_title($post_id) {
      $post_type = get_post_type($post_id);
      if ($post_type != 'car') {
        return;
      }
      $post_title = get_field('title', $post_id);
      $post_name = sanitize_title($post_title);
      $post = array(
        'ID' => $post_id,
        'post_name' => $post_name,
        'post_title' => $post_title
      );
      wp_update_post($post);
    }
    
    

    Is that’s the way to do it? Or there’s better way?

    Thanks