How to replace post name slug with post ID in Custom Post Types

Here is a way to replace the post slug with the post ID in a custom post type permalink structure.

Example.

Change

somedomain.com/some-permalink 

To

somedomain.com/123

Assuming the post type already exists with

'rewrite' => array('slug' => 'book') ...
/**
 * Add new rewrite for post type
 * Add permalink structure
 */
function book_post_type_rewrite() {
    global $wp_rewrite;
 
    // Set the query arguments used by WordPress
    $queryarg = 'post_type=book&p=';
 
    // Concatenate %book_id% to $queryarg (eg.. &p=123)
    $wp_rewrite->add_rewrite_tag( '%book_id%', '([^/]+)', $queryarg );
 
    // Add the permalink structure
    $wp_rewrite->add_permastruct( 'book', '/book/%book_id%/', false );
}
add_action( 'init', 'book_post_type_rewrite' );
 
/**
 * Replace permalink segment with post ID
 *
 */
function book_post_type_permalink( $post_link, $id = 0, $leavename ) {
    global $wp_rewrite;
    $post = get_post( $id );
    if ( is_wp_error( $post ) )
        return $post;
 
        // Get post permalink (should be something like /book/%book_id%/
        $newlink = $wp_rewrite->get_extra_permastruct( 'book' );
 
        // Replace %book_id% in permalink structure with actual post ID
        $newlink = str_replace( '%book_id%', $post->ID, $newlink );
        $newlink = home_url( user_trailingslashit( $newlink ) );
    return $newlink;
}
add_filter('post_type_link', 'book_post_type_permalink', 1, 3);

Refresh permalinks

Refer http://stackoverflow.com/questions/25199501/wordpress-custom-type-append-post-id-at-the-end?answertab=votes#tab-top