Home › Forums › General Issues › Change the title of a custom post type to match filed value?
I have an ACF text field for custom post type and I would like it to be the title of my post.
Is possible to do that? In the specific is it possible to change in backend the title of a post with the value of an ACF text filed in that post?
add_action('acf/save_post', 'acf_field_to_title', 20);
function acf_field_to_title($post_id) {
remove_filter('acf/save_post', 'acf_field_to_title', 20);
$title = get_field('your-field', $post_id);
if ($title) {
$post = get_post($post_id);
$post->post-title = $title;
wp_update_post($post);
}
add_action('acf/save_post', 'acf_field_to_title', 20);
}
When I add this to my functions.php (I am using a Child theme) I get an error.
In the specific the error says syntax error, unexpected '=' ... on linke 54
On that line there is this
$post->post-title = $title;
BTW: I would like that all the posts are saved with the Value in the “title” field
I typed it here where there is no syntax checking, should be
$post->post_title = $title;
Now seems to work. Do I need to re-save all my posts again manually in order to have this rules applied?
May I add something to my question? Maybe it explain better my issue:
I am getting those posts in my CPT from somewhere (is an external software). The posts are automatically saved in WP with a title that looks like: title-one-2345
Is it possible to apply the rule you proved above automatically?
Yes you would need to re-save all posts
To do this automatically your would need to hook into whatever it is that is saving these post in the first place and the field would need to exist already being created by whatever is creating the posts. What I’ve given you will only work when editing a post in the admin.
You must be logged in to reply to this topic.
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.