Support

Account

Home Forums Backend Issues (wp-admin) Generate post title from two ACF fields Reply To: Generate post title from two ACF fields

  • 
    // run function after acf updates fields
    add_action('acf/update_post', 20);
    function title_from_fields($post_id) {
      // check for your post type
      if (get_post_type($post_id) != 'job_updates')) {
        return;
      }
      // get the post
      $post = get_post($post_id);
      // get the taxonomy field
      $terms = get_field('job', $post_id);
      // taxonomy fields return an array, use the first term
      $term = $terms[0];
      // get date field
      $date = get_field('date', $post_id);
      // set post title/slug
      $title = $term->name.' '.$date;
      $post->post_title = $title;
      $post->post_name = sanitize_title($title);
      // remove this filter to prevent infinite loop
      remove_filter('acf/update_post', 20);
      // update the post
      wp_update_post($post);
      // re-add this filter
      add_action('acf/update_post', 20);
    }