You need to set the ID
for the post_title
and post_name
. You have many approaches.
Typically your approach would be to use a filter in WordPress. The best I can say would be wp_insert_post_data
and use the second parameter of this filter to get the post id and return the first parameter with the modified post_title
and post_name
.
The other option would be to use actions either save_post
or wp_insert_post
They are defined with the same arguments.
File: wp-includes/post.php
3496: /**
3497: * Fires once a post has been saved.
3498: *
3499: * @since 1.5.0
3500: *
3501: * @param int $post_ID Post ID.
3502: * @param WP_Post $post Post object.
3503: * @param bool $update Whether this is an existing post being updated or not.
3504: */
3505: do_action( 'save_post', $post_ID, $post, $update );
3506:
3507: /**
3508: * Fires once a post has been saved.
3509: *
3510: * @since 2.0.0
3511: *
3512: * @param int $post_ID Post ID.
3513: * @param WP_Post $post Post object.
3514: * @param bool $update Whether this is an existing post being updated or not.
3515: */
3516: do_action( 'wp_insert_post', $post_ID, $post, $update );
This may be from where you can start. Note that using wp_update_post
at the end of _20170104_02
function will fire our save_post
action again, so we needed to remove that action to escape from the infinite loop.
If you would use wp_insert_post_data
this would not be needed, since filters return the data.
add_action('save_post', '_20170104_02', 10, 3);
function _20170104_02( $post_id, $post, $update ){
if ( 'cpt' != $post->post_type) // only for your custom post type cpt
return;
if ( wp_is_post_revision( $post_id ) )
return;
if ( wp_is_post_autosave( $post_id ) )
return;
if ( !( true == $update && 'publish' == $post->post_status ) )
return;
remove_action( 'save_post', '_20170104_02' );
$my_post = array(
'ID' => $post_id,
'post_title' => $post_id,
'post_name' => $post_id
);
wp_update_post( $my_post );
}