I am using custom posts, and in these, I don’t a need for the title.
This causes WordPress to set the titles of my posts to “Auto Draft”.
I’d like to change the title’s value to something else, computed from other fields in my post.
How do I go about doing that using save_post or some other means?
This simplest method would be to edit the data at the point it’s inserted, rather than updating it afterwards, using
wp_insert_post_data
instead ofsave_post
. This works on creating a new post or updating an existing post without change. It also avoids the danger of creating an infinite loop by triggeringupdate_post
withinsave_post
.I had the exact same need, so I wrote this function – which works. Modify it to your needs. Hope this helps.
Here’s a solution that uses a static variable to prevent an infinite loop. This allows you to safely call
wp_update_post()
inside of a function that is hooked tosave_post
.Note: To limit this functionality to a certain post type, use the save_post_{$post->post_type}
hook instead of save_post.
Try the filter default_title: