For a given Custom Post Type, I'd like that every time I create a new post (by using the Dashboard, clicking the button "Add new"), that the slug and Title assume this post's ID.

I belive that the ID is only assigned when the post is saved (draft or published), and the slug is only assigned when the status is published... so, how could I accoplish this?

The reason is simple : the custom post types are to be anonymous, and the post ID was the simplest approach I could think of, but I'm open to alternatives.

  • @prosti I fail to see how the short answers above do anything to help me solve this... Jan 4 '17 at 5:10
  • There may be more @Jean-FrancoisArseneault questions you may ask, but I provided you one possible answer.
    – prosti
    Jan 4 '17 at 18:10

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 );
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

    if ( wp_is_post_revision( $post_id ) )

    if ( wp_is_post_autosave( $post_id ) )

    if ( !( true == $update  && 'publish' == $post->post_status ) )

    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 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.