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


Generate post title from two ACF fields

  • This is probably so simple for you all…

    I have a custom post type called Job Updates (slug is job_updates)

    It has 3 fields…

    Job – a taxonomy
    Date – date picker
    Update – WYSIWYG

    I would like the title of the post to auto generate based on the Job + Date
    So if the job is Sherman Water Damage, it would be Sherman Water Damage 6-1-2022

    I’ve searched the forum and tried several different functions in the Snippets app, while swapping out their variables for mine, but none seem to work. I don’t know php, so that might be part of it.

    // 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')) {
      // 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
      // re-add this filter
      add_action('acf/update_post', 20);
  • Thank you for this suggestion.
    For some reason it throws a fatal error from line 5
    if (get_post_type($post_id) != ‘job_updates’)) {

  • There is a typo in my code, the line should be

    if (get_post_type($post_id) != 'job_updates') {
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.