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?
Hi, thanks so much for this. Helped a lot! But I need some help here, please.
I want my title to have not only the ACF field value, but also the custom taxonomy I’ve created called “pico”. When I run this code,
function my_acf_update_value( $value, $post_id, $field ) {
$terms = wp_get_post_terms($post_id, 'pico');
$pico = false;
foreach($terms as $term)
{
if($term->parent)
{
$parent = get_term($term->parent, 'pico');
$pico = $term->name;
break;
}
}
//Default to first selected term name if no children were found
$pico = $pico ? $pico : $terms[0]->name;
$new_title = $pico . ' - ' . $value;
$new_slug = sanitize_title( $new_title );
// Update post
$my_post = array(
'ID' => $post_id,
'post_title' => $new_title,
'post_name' => $new_slug,
);
// Update the post into the database
wp_update_post( $my_post );
return $value;
}
add_filter('acf/update_value/name=data_do_boletim', 'my_acf_update_value', 10, 3);
it gives me these error messages:
Notice: Undefined offset: 0 in /Users/Mauricio/Sites/RicoSurf/wp-content/themes/ricosurf/functions.php on line 219
Notice: Trying to get property of non-object in /Users/Mauricio/Sites/RicoSurf/wp-content/themes/ricosurf/functions.php on line 219
Warning: Cannot modify header information – headers already sent by (output started at /Users/Mauricio/Sites/RicoSurf/wp-content/themes/ricosurf/functions.php:219) in /Users/Mauricio/Sites/RicoSurf/wp-includes/pluggable.php on line 1196
But when I go back and see that post, the title is perfect, exactly the way I want.
So I assume the code “is working” just not 100%. Can some one help me find out what’s wrong?
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!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?
— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.