Home › Forums › Backend Issues (wp-admin) › Replacing custom post type post title with an acf? › Reply To: Replacing custom post type post title with an acf?
Follow up to my own question. I’ve got this working as desired, but I want to post my code below in case it’s useful to anyone, and to make sure I’m doing this right.
function sk_acf_update_pocket_postdata( $value, $post_id, $field ) {
global $_POST;
$pocket_name_fields = get_field('pocket_name', $post_id)[0];
$head_brand = $pocket_name_fields['head_brand'];
$head_name = $pocket_name_fields['head_name'];
$pocket_title = $head_brand . ' ' . $head_name . ' ' . $value;
$pocket_slug = sanitize_title( $pocket_title );
$pocket_postdata = array(
'ID' => $post_id,
'post_title' => $pocket_title,
'post_name' => $pocket_slug
);
wp_update_post( $pocket_postdata );
return $value;
}
add_filter('acf/update_value/key=field_53f7a5369c2ab', 'sk_acf_update_pocket_postdata', 10, 3);
I changed the field key I’m hooking into to the last sub field in the repeater. If I used the other two, it would only rewrite the title and slug after hitting update for a second time.
Still seems a little strange and inelegant, but it’s working. I welcome feedback of any kind.
BTW, thanks @Elliot for building and maintaining such an incredible tool. It has completely changed how I work.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.