I want to make the slug for each of my custom post types to be the post ID number. How do I do this? None of my posts accept post title, content, or anything else - they require different data which was easier to display as a meta box). To illustrate:

I have a CPT ('eduation'). When I submit the post into the database, it has an ID of, say, 15. When I visit mysite.com/edu/15 ('edu' being the post slug that I have set), I see the details from this post in my site.

I know I could simply change the permalinks, but I want those to stay as they are; I just want to change the ones for these custom post types. Sorry if the question is confusing. It probably is really simple to answer, but I really don't know what the answer is.

1 Answer 1


Here's a way how to change the slug:

add_action('wp_insert_post', 'change_slug');
function change_slug( $post_id ) {

       // Making sure this runs only when a 'eduation' post type is created
       $slug = 'eduation';
       if ( $slug != $_POST['post_type'] ) {

       // `wp_update_post` calls `wp_insert_post`. Prevent infinite recursions!
       $post = get_post($post_id);
       if ($post->post_name == $post_id) return;

       wp_update_post( array(
        'ID' => $post_id,
        'post_name' => $post_id // slug

  • Thanks for that. Would I be right to assume that $post_type works for checking if it is the right post type?
    – Nate
    Sep 7, 2014 at 0:59
  • Never mind, it's 2am in the UK, and I'm starting to get a little tired. I didn't notice that you check for the post type in the if statement. Thanks for this. I'll give it a whirl and let you know what happens.
    – Nate
    Sep 7, 2014 at 1:00
  • That works perfectly. Although all of my posts are now named "Auto Draft". Given the time, I shall look tomorrow for how to fix that. I didn't think of that at all, but I guess it would be easy to edit. I'm sure I've done that before, based on the meta data, so I shall check one of my past projects.
    – Nate
    Sep 7, 2014 at 1:06
  • @Nate regarding the "Auto Draft" title: you already set up the "ID" and "slug" of the post, in the` wp_update_post` function from @Erevald but not the post itile. You should be able to set a new title the same way here with 'post_title' => $my_new_title_here,.
    – LWS-Mo
    Sep 1, 2017 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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