Support

Account

Home Forums General Issues Create/update post title from ACF fields

Solved

Create/update post title from ACF fields

  • Hello together,
    I want to set the post title myself with custom fields.
    For this I had found this post here:
    https://support.advancedcustomfields.com/forums/topic/createupdate-post-title-from-acf-fields/

    I have created 2 custom plugin folders for my custom post types.

    As long as only one plugin is active, this works, or always save with the higher priority.

    First Plugin with priority 15

    add_action('acf/save_post', 'mitarbeiter_acf_speichern',15);
    function mitarbeiter_acf_speichern($post_id) {
        $mein_post = array();
        $mein_post['ID'] = $post_id;
        $nachname = get_field('nachname');
        $vorname = get_field('vorname');
        $mein_post['post_title'] = $vorname  . ' ' . $nachname;
    
        // Save/Update Post
        wp_update_post( $mein_post );
    }

    Second Plugin with priority 16

    // Wird nachdem speichern ausgeführt
    add_action('acf/save_post', 'kantine_acf_speichern',16);
    function kantine_acf_speichern($post_id) {
        $kantine_post = array();
        $kantine_post['ID'] = $post_id;
        $myvar = get_field('menu01');
        $kantine_post['post_title'] = $myvar;
        
        wp_update_post( $kantine_post );
    }

    Can anyone give me a hint?

  • You need to have conditions on when the title is generated. In your case the second action with a priority of 16 will always run second. Since the fields of the other post type do not exist get field returns NULL and set the title to nothing.

    YOu need to do something like

    
    if (get_post_type($post_id) == 'your-cpt-name')) {
       // set post title
    }
    

    or

    
    if (get_field('your-field-name', $post_id)) {
      // the field has a title, set post title from this field.
    }
    
  • Thank you John, it helped to check the field if it is empty.

    
    if (get_field('your-field-name', $post_id)) {
      // the field has a title, set post title from this field.
    }
    
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.