I did not work it out using save_post or any other save-filter, so as I am only needing to do the same for the title, I used the default_title filter. This means that it works for drafts as well.
add_filter( 'default_title', 'change_title', 10, 2 );
function change_title( $title, $post ) {
return 'PO' . $post->ID;
}
Notice that here I am using the newly created post’s ID, so I don’t have to fetch $last_post_id as you do, and perhaps you don’t have to do that either if you do it like this. Remember to check the post type before changing the title though.
I have not tested to do the same with the slug, but I noticed there is a similar filter for the slug, called wp_unique_post_slug. Hopefully this will help you. 🙂
Edit: here is the filter from WP Core code, https://developer.wordpress.org/reference/functions/wp_unique_post_slug/
Related Posts:
- Get post ID from wp_insert_post()
- WP insert post and custom taxonomy
- How do I programmatically add items of content to a custom post type?
- Not Able to Insert Taxonomy Term Using wp_insert_post()
- How I upload, save and set a featured image from my frontend?
- Trying to save custom post type from frontend partially working
- wp_insert_post custom taxonomy
- Assign category to front end post
- How can I programmatically save data into custom fields that contain serialized data?
- What hook should be used to programmatically create a post only when master post is updated?
- WordPress tax_input only if logged in
- Programmatically adding posts
- Is it possible to create a post using a metabox?
- How Can I Set the Post Author of a Post I Just Created With PHP?
- Auto-generated posts not showing in backend (but being counted!)
- wp_insert_post generates endless posts
- create parent post using wp_insert_post
- Cron job for creating posts not excecuting properly
- How to checked selected category checkbox when my custom post inserted from frontend
- Post author for revision not being set on update/insert
- Add CPT values to Database
- Why cant I add a custom post type to a custom taxonomy?
- wp_insert_post wrong post type [closed]
- Programatically inserted posts not showing in Posts Table
- Is it possible to add posts and images (auto resize) for custom post types with a script?
- Update post if exist from PHP
- WordPress hook which triggers on post import
- How to handle new post from API request?
- How to handle this specific case of custom post type?
- insert image with custom post type
- importing table data to custom post type
- How can I get some of a posts meta data whilst it is still inserting?
- See if the email exists or not
- WP_Insert_Post creating duplicate posts when logged in
- wp_insert_post from XML feed only inserting first post
- What is the correct way that when creating a custom post type assign values to custom fields created with pods framework?
- How do I find out which (page) template file my custom child post is looking for?
- wp_insert_post deleting previous post custom meta
- Published page but it is page not found
- How to insert post 6 times after user register like ask.fm
- wp_insert_post() creates duplicates with ‘post_status’ => ‘publish’
- automatic creation of custom post type articles
- Submitting custom post from frontend,jQuery ajax, and custom validation – can’t find proper place to insert wp_insert_post()
- UPDATED: Save a custom_field value when automatically creating a post using wp_insert_post
- Generate slug and meta data if meta field is empty
- Add a image to a post published on a CPT from the front-end
- wordpress form processing to custom post type not working
- install.php Custom Taxonomy Term not being added to custom post
- Omitted Content After Post-Publish Insertion
- Is this the correct way to add post-slug input field?
- wp_insert_post not working for custom post type?
- After wp_insert_post() custom post type does not show in the admin
- Using wp_insert_post to create custom posts with ACF image field
- Add a post when another post type is inserting
- Custom Post Types and template_redirect
- How can I bulk upload images and automatically create posts for each one at the same time?
- Allowing custom role access to custom post type in back end
- Best way to flush_rewrite_rules for custom post type, in a mu-plugins plugin?
- Automatically set post title to same value as a meta box
- Custom Post Type Archives by Date and Taxonomy
- Drag n Drop Post Order for multiple Custom Post Types
- How to get all custom fields of any post type
- How to put custom post types on front page
- array_pop() expects array, boolean given for $event_type_term ->slug
- Using %postname% tag with a Custom Permastruct creates 400 Bad Request Errors from the server
- Adding Categories across posts, and custom post types
- Get template part based on custom taxonomy term
- WordPress returns 404 on custom rewrite rule conflict in parameters
- custom post types, wp_get_archives and add_rewrite_rule
- Title_save_pre – Simple problem that u know for sure
- How to redirect to a passworded page based on password entered on login screen
- wp_publish_post breaks permalinks
- Unable to save or update postmeta for custom post type via action using save_post hook
- How can we disable a custom post type archive page but enable its feed?
- Add text to post list/edit screens?
- How do you output custom code between posts in the loop?
- Disable comments from showing up public for Custom Post Type
- Get the posttype of a taxonomy/term
- Delete Current Author Frontend user while delete custom post type
- Add hierarchical taxonomy to permalink for custom post type
- Redeclare slug & name of custom post type?
- Change name of custom post type archive
- Editable content on a Custom Archive page
- Parent field missing from custom post type in the block editor
- Custom posts don’t work
- Pagination is not working in custom post type
- Custom Post Types. Are there any disadvantages/advantages in using a plugin to develop them?
- allow BOTH post slug and meta key value in permalink for custom post type
- A simple script to allow sorting of custom posts in admin?
- Is it possible to create relational metabox values in a custom post?
- Custom Post Type Query for Sidebar Doesn’t Work on Front Page
- Two Custom Post Types Many to Many Relationship
- URL rewrite add author as base
- filter custom post in rest api with custom function
- Single post with a Custom Post Type returns 404, when archives of the CPT work correctly
- Custom Post Type slug same as page name
- manage_edit-{post_type}_sortable_columns sorts, but wrong!
- Custom Post Type Pagination Paginates Only in URL Structure
- Deny user access to edit post while allow him to edit custom post type
- Ordering custom post type by multiple custom fields