Support

Account

Home Forums ACF PRO Using ACF fields to setup a post title

Solved

Using ACF fields to setup a post title

  • Hello,

    I’m developping a plugin for a booking agency. I have registered 3 custom post types (“band, “show” and “venue”) :

    // Register Band Post Type
    function band_post_type() {
    
            $labels = array(
                    'name'                  => _x( 'Bands', 'Post Type General Name', 'doomstar' ),
                    'singular_name'         => _x( 'Band', 'Post Type Singular Name', 'doomstar' ),
                    'menu_name'             => __( 'Bands', 'doomstar' ),
                    'name_admin_bar'        => __( 'Band', 'doomstar' ),
                    'archives'              => __( 'Band Archives', 'doomstar' ),
                    'attributes'            => __( 'Band Attributes', 'doomstar' ),
                    'parent_item_colon'     => __( 'Parent Band:', 'doomstar' ),
                    'all_items'             => __( 'Bands', 'doomstar' ),
                    'add_new_item'          => __( 'Add New Band', 'doomstar' ),
                    'add_new'               => __( 'Add New', 'doomstar' ),
                    'new_item'              => __( 'New Band', 'doomstar' ),
                    'edit_item'             => __( 'Edit Band', 'doomstar' ),
                    'update_item'           => __( 'Update Band', 'doomstar' ),
                    'view_item'             => __( 'View Band', 'doomstar' ),
                    'view_items'            => __( 'View Bands', 'doomstar' ),
                    'search_items'          => __( 'Search Band', 'doomstar' ),
                    'not_found'             => __( 'Not found', 'doomstar' ),
                    'not_found_in_trash'    => __( 'Not found in Trash', 'doomstar' ),
                    'featured_image'        => __( 'Featured Image', 'doomstar' ),
                    'set_featured_image'    => __( 'Set featured image', 'doomstar' ),
                    'remove_featured_image' => __( 'Remove featured image', 'doomstar' ),
                    'use_featured_image'    => __( 'Use as featured image', 'doomstar' ),
                    'insert_into_item'      => __( 'Insert into band', 'doomstar' ),
                    'uploaded_to_this_item' => __( 'Uploaded to this band', 'doomstar' ),
                    'items_list'            => __( 'Bands list', 'doomstar' ),
                    'items_list_navigation' => __( 'Bands list navigation', 'doomstar' ),
                    'filter_items_list'     => __( 'Filter bands list', 'doomstar' ),
            );
            $args = array(
                    'label'                 => __( 'Band', 'doomstar' ),
                    'labels'                => $labels,
                    'supports'              => array( 'title', 'editor', 'thumbnail', ),
                    'taxonomy'              => array( 'roster', ),
                    'hierarchical'          => false,
                    'public'                => true,
                    'show_ui'               => true,
                    'show_in_menu'          => true,
                    'menu_position'         => 5,
                    'menu_icon'             => 'dashicons-groups',
                    'show_in_admin_bar'     => true,
                    'show_in_nav_menus'     => true,
                    'can_export'            => true,
                    'has_archive'           => 'roster',
                    'exclude_from_search'   => false,
                    'publicly_queryable'    => true,
                    'capability_type'       => 'post',
                    'show_in_rest'          => true,
                    'edit_in_rest'          => true,
            );
            register_post_type( 'band', $args );
    
    }
    add_action( 'init', 'band_post_type', 0 );
    
    // Register Show Post Type
    function show_post_type() {
    
            $labels = array(
                    'name'                  => _x( 'Shows', 'Post Type General Name', 'doomstar' ),
                    'singular_name'         => _x( 'Show', 'Post Type Singular Name', 'doomstar' ),
                    'menu_name'             => __( 'Shows', 'doomstar' ),
                    'name_admin_bar'        => __( 'Show', 'doomstar' ),
                    'archives'              => __( 'Show Archives', 'doomstar' ),
                    'attributes'            => __( 'Show Attributes', 'doomstar' ),
                    'parent_item_colon'     => __( 'Parent Show:', 'doomstar' ),
                    'all_items'             => __( 'Shows', 'doomstar' ),
                    'add_new_item'          => __( 'Add New Show', 'doomstar' ),
                    'add_new'               => __( 'Add New', 'doomstar' ),
                    'new_item'              => __( 'New Show', 'doomstar' ),
                    'edit_item'             => __( 'Edit Show', 'doomstar' ),
                    'update_item'           => __( 'Update Show', 'doomstar' ),
                    'view_item'             => __( 'View Show', 'doomstar' ),
                    'view_items'            => __( 'View Shows', 'doomstar' ),
                    'search_items'          => __( 'Search Show', 'doomstar' ),
                    'not_found'             => __( 'Not found', 'doomstar' ),
                    'not_found_in_trash'    => __( 'Not found in Trash', 'doomstar' ),
                    'featured_image'        => __( 'Featured Image', 'doomstar' ),
                    'set_featured_image'    => __( 'Set featured image', 'doomstar' ),
                    'remove_featured_image' => __( 'Remove featured image', 'doomstar' ),
                    'use_featured_image'    => __( 'Use as featured image', 'doomstar' ),
                    'insert_into_item'      => __( 'Insert into show', 'doomstar' ),
                    'uploaded_to_this_item' => __( 'Uploaded to this show', 'doomstar' ),
                    'items_list'            => __( 'Shows list', 'doomstar' ),
                    'items_list_navigation' => __( 'Shows list navigation', 'doomstar' ),
                    'filter_items_list'     => __( 'Filter shows list', 'doomstar' ),
            );
            $args = array(
                    'label'                 => __( 'Show Date', 'doomstar' ),
                    'labels'                => $labels,
                    'supports'              => array( 'title', 'thumbnail', ),
                    'taxonomies'            => array( 'event status', ),
                    'hierarchical'          => false,
                    'public'                => true,
                    'show_ui'               => true,
                    'show_in_menu'          => true,
                    'menu_position'         => 5,
                    'menu_icon'             => 'dashicons-calendar',
                    'show_in_admin_bar'     => true,
                    'show_in_nav_menus'     => true,
                    'can_export'            => true,
                    'has_archive'           => 'show',
                    'exclude_from_search'   => false,
                    'publicly_queryable'    => true,
                    'capability_type'       => 'post',
                    'show_in_rest'          => true,
                    'edit_in_rest'          => true,
            );
            register_post_type( 'show', $args );
    
    }
    add_action( 'init', 'show_post_type', 0 );
    
    // Register Venue Post Type
    function venue_post_type() {
    
            $labels = array(
                    'name'                  => _x( 'Venues', 'Post Type General Name', 'doomstar' ),
                    'singular_name'         => _x( 'Venue', 'Post Type Singular Name', 'doomstar' ),
                    'menu_name'             => __( 'Venues', 'doomstar' ),
                    'name_admin_bar'        => __( 'Venue', 'doomstar' ),
                    'archives'              => __( 'Venue Archives', 'doomstar' ),
                    'attributes'            => __( 'Venue Attributes', 'doomstar' ),
                    'parent_item_colon'     => __( 'Parent Venue:', 'doomstar' ),
                    'all_items'             => __( 'Venues', 'doomstar' ),
                    'add_new_item'          => __( 'Add New Venue', 'doomstar' ),
                    'add_new'               => __( 'Add New', 'doomstar' ),
                    'new_item'              => __( 'New Venue', 'doomstar' ),
                    'edit_item'             => __( 'Edit Venue', 'doomstar' ),
                    'update_item'           => __( 'Update Venue', 'doomstar' ),
                    'view_item'             => __( 'View Venue', 'doomstar' ),
                    'view_items'            => __( 'View Venues', 'doomstar' ),
                    'search_items'          => __( 'Search Venue', 'doomstar' ),
                    'not_found'             => __( 'Not found', 'doomstar' ),
                    'not_found_in_trash'    => __( 'Not found in Trash', 'doomstar' ),
                    'featured_image'        => __( 'Featured Image', 'doomstar' ),
                    'set_featured_image'    => __( 'Set featured image', 'doomstar' ),
                    'remove_featured_image' => __( 'Remove featured image', 'doomstar' ),
                    'use_featured_image'    => __( 'Use as featured image', 'doomstar' ),
                    'insert_into_item'      => __( 'Insert into venue', 'doomstar' ),
                    'uploaded_to_this_item' => __( 'Uploaded to this venue', 'doomstar' ),
                    'items_list'            => __( 'Venues list', 'doomstar' ),
                    'items_list_navigation' => __( 'Venues list navigation', 'doomstar' ),
                    'filter_items_list'     => __( 'Filter venues list', 'doomstar' ),
            );
            $args = array(
                    'label'                 => __( 'Venue', 'doomstar' ),
                    'labels'                => $labels,
                    'supports'              => array( 'title', ),
                    'hierarchical'          => false,
                    'public'                => true,
                    'show_ui'               => true,
                    'show_in_menu'          => 'edit.php?post_type=show',
                    'show_in_admin_bar'     => true,
                    'show_in_nav_menus'     => true,
                    'can_export'            => true,
                    'has_archive'           => false,
                    'exclude_from_search'   => false,
                    'publicly_queryable'    => true,
                    'capability_type'       => 'post',
                    'show_in_rest'          => true,
                    'edit_in_rest'          => true,
            );
            register_post_type( 'venue', $args );
    
    }
    add_action( 'init', 'venue_post_type', 0 );

    I have created a field group for the custom post type “show” with the following fields (label, name and type):

    • Band, band , Post Object
    • Date, date, Date Picker
    • Country, country, Select
    • City, city, Text
    • Venue, venue, Post Object
    • Additional Info, additional_info, Text
    • Ticket URL, ticket_url, Url

    I’m trying to programmatically setup the post title of custom posts “show”. I’m using this function :

    // Auto-populate post title with ACF.
    function show_update_postdata( $value, $post_id, $field ) {
    
            // Get fields value
            $date = strtotime(get_field('date', $post_id));
            $band = get_the_title(get_field('band', $post_id));
            $country = get_field('country')['value'];
            $city = get_field('city');
    
            // Custom post title
            $formatted_date = date_i18n('F jS, Y', $date);
            $title = $band . ' @ ' . $city . ', ' . $country . ' - ' . $formatted_date;
            $slug = sanitize_title( $title );
            $postdata = array(
                 'ID'          => $post_id,
                 'post_title'  => $title,
                 'post_type'   => 'show',
                 'post_name'   => $slug
            );
    
            wp_update_post( $postdata, true );
            return $value;
    
    }
    add_filter('acf/update_value/name=date', 'show_update_postdata', 10, 3);
    add_filter('acf/update_value/name=band', 'show_update_postdata', 10, 3);
    add_filter('acf/update_value/name=city', 'show_update_postdata', 10, 3);
    add_filter('acf/update_value/name=country', 'show_update_postdata', 10, 3);

    My problem is that the post title isn’t set properly when I hit the “Publish” button. I need to save the post twice in order to get a proper post title.



    What am I missing?

    I’m using WordPress 4.8 and ACF Pro 5.5.14. Let me know if you need more details.

    Thank you,
    Cedric

  • I’ve managed to get it working by retrieving fields values from the POST request.

    function show_update_postdata( $value, $post_id, $field ) {
    
            // Get values from POST
            $date = strtotime($_POST['acf']['field_595cf97c444c0']);
            $band = get_the_title($_POST['acf']['field_595cfa314cf4b']);
            $country = $_POST['acf']['field_595cf99f444c1'];
            $city = $_POST['acf']['field_595cf9f1444c2'];
    
            // Custom post title
            $formatted_date = date_i18n('F jS, Y', $date);
            $title = $band . ' @ ' . $city . ', ' . $country . ' - ' . $formatted_date;
            $slug = sanitize_title( $title );
            $postdata = array(
                 'ID'          => $post_id,
                 'post_title'  => $title,
                 'post_type'   => 'show',
                 'post_name'   => $slug
            );
    
            wp_update_post( $postdata );
            return $value;
    
    }
    add_filter('acf/update_value/name=date', 'show_update_postdata', 10, 3);
    add_filter('acf/update_value/name=band', 'show_update_postdata', 10, 3);
    add_filter('acf/update_value/name=city', 'show_update_postdata', 10, 3);
    add_filter('acf/update_value/name=country', 'show_update_postdata', 10, 3);
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Using ACF fields to setup a post title’ is closed to new replies.