Support

Account

Home Forums Backend Issues (wp-admin) How can I copy Worpdress title to an ACF field?

Unread

How can I copy Worpdress title to an ACF field?

  • I have a field, for example: deatils.
    The post title has a pattern, for example: Title – details. It has two parts, the separator is a ‘-‘ character. The first part is only a prefix, but the second is important for other reasons.
    How can I copy only the second part to the ‘details’ field?
    I tried with this code snippet, but it didn’t work.

    
    function my_custom_title_operation( $value, $post_id, $field ) {
        $title = get_the_title($post_id);
        if (strpos($title, '-') !== false) {
            $parts = explode('-', $title, 2);
            if (isset($parts[1])) {
                $after_dash = trim($parts[1]);
    
                add_post_meta($post_id, 'details', $after_dash);
            }
        }
        return $value;
    }
    add_filter('acf/update_value/name=details', 'my_custom_title_operation', 10, 3);
    
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.