Support

Account

Home Forums Feature Requests Set 'Post Title' from Field Group (Toggle) Reply To: Set 'Post Title' from Field Group (Toggle)

  • I’ve been playing around with this code a bit.
    But still experience 1 small issue with revisions.

    When posting a new page you always get 2 versions of the page.
    Version 1 sets all the acf data, and then version 2 changes the title.
    Would there be any way to hide the second version, or combine the 2?

    For prosperity, here’s the code I’ve been using in functions.php:

    /*  ACF Title Updater
    /* ------------------------------------ */
    // add_action( 'init', 'calmah_remove_post_title', 10 );
    function calmah_remove_post_title() {
        remove_post_type_support( 'post', 'title' );
    }
    
    add_action('acf/save_post', 'calmah_title_updater', 20);
    function calmah_title_updater($post_id) {
        $title = '';
        
        switch (get_post_type($post_id)) {
            case 'news':
                $title = get_field('nl_title', $post_id); break;
            case 'events':
                $title = get_field('date_start', $post_id) . ': ' . get_field('nl_title', $post_id); break;
            case 'album':
                $title = get_field('nl_title', $post_id) . '(' . get_field('location', $post_id) . ')'; break;
            case 'members':
                $title = get_field('first_name', $post_id) . ' ' . get_field('family_name', $post_id); break;
        }
        
        if (!empty($title)) {
            $slug = sanitize_title($title);
        	$my_post = array(
    			'ID' => $post_id,
    			'post_title' => $title,
    			'post_name' => $slug
    		);
    		wp_update_post($my_post);
        }
    }