• momochii95

    (@momochii95)


    Hello,

    I would like to know how to implement ACF custom field to default post title so the title could go from
    (default title)
    to
    (Chapter type) (chapter number).(chapter part if any): (default title)

    For eg:
    Half Blood
    to
    Chapter 55.1: Half Blood

    Is there a code for it?
    Or at least a guide to do so?

    Many thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi,

    got following code from amazing ACF (advancedcustomfields) Support, try it in your child themes functions.php

    This worked on single value fields and still not on taxonomy fields. We are also trying to find a solution for multi select fields or even multi select taxonomy fields. Any solution would be appreciated..

    /**
    * Generate post title and post slug from ACF single value fields
    *
    */
    
    add_action('acf/save_post', 'my_acf_save_post', 5);
    function my_acf_save_post( $post_id ) {
    
        $post_title = 'My default title';
        $slug = 'my-default-slug';
    
        // Check if a specific field value was updated.
        // Replace field_key with the actual field keys of the fields
        if( isset($_POST['acf']['field_key1']) && isset($_POST['acf']['field_key2']) && isset($_POST['acf']['field_key3']) && isset($_POST['acf']['field_key4']) && isset($_POST['acf']['field_key5'])) {
            
            // Replace field_key with keys of the fields needed to generate title
            $post_title = $_POST['acf']['field_key1'] . ' ' . $_POST['acf']['field_key2'] . ' ' . $_POST['acf']['field_key3'] . ' ' . $_POST['acf']['field_key4'] . ' ' . $_POST['acf']['field_key5'];
            $slug = sanitize_title( $post_title );
        }
    
        // Grab Post Data from the Form
        $post = array(
            'ID'           => $post_id,
            'post_title'   => $post_title,
            'post_name'    => $slug,
        );
    
        // Update the Post
        wp_update_post( $post );
    }
    • This reply was modified 1 year, 12 months ago by condraw.
    • This reply was modified 1 year, 12 months ago by condraw.
    Thread Starter momochii95

    (@momochii95)

    Hi,
    I tried the code but now the title just stuck as “My default title” and the code didn’t change to what I want

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    Your code looks fine, but I would recommend to log your variables using acf_log() or print_r() in order to make sure that your post id is correct and your post_title is correctly generated.

    wp_update_post() will create a post instead of updating when the post id is not correctly defined (See documentation), that might come from here.

    I can’t really provide further support here, as the question is related to the native ACF hook acf/save_post (See documentation), and isn’t really about the ACF Extended plugin itself.

    You can try to open a thread on the ACF Support Forum, there are a lot of ACF developers here which might help you with your case.

    Hope it helps!

    Have a nice day!

    Regards.

    I found this code here, maybe it can help you.

    I wanted to change the post title (post-title) to a value defined in a CPT called custom_title.

    Here is the code, maybe this is what you are looking for:

    // Update Post Title Value
    add_filter('acf/update_value/name=my_field', 'my_acf_post_title_save_value', 10, 3);
    function my_acf_post_title_save_value($value, $post_id, $field){
    
        // Retrieve Post ID Info
        $data = acf_get_post_id_info($post_id);
    
        // Bail early if not Post
        if($data['type'] !== 'post')
            return $value;
    
        wp_update_post(array(
            'ID'            => $data['id'],
            'post_title'    => $value,
        ));
    
        return $value;
    
    }
    
    // Load Post Title Value
    add_filter('acf/load_value/name=my_field', 'my_acf_post_title_load_value', 10, 3);
    function my_acf_post_title_load_value($value, $post_id, $field){
    
        // Retrieve Post ID Info
        $data = acf_get_post_id_info($post_id);
    
        // Bail early if not Post
        if($data['type'] !== 'post')
            return $value;
    
        // Retrieve current Post Title
        $value = get_post_field('post_title', $data['id']);
    
        return $value;
    
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to create title post from ACF custom field value’ is closed to new replies.