I have a custom field that I would like to use as the post's slug. I'm able to get the custom field to be used as the slug for the post but I'm having problems attaching a /prefix/ before the postname.

This is the code I have so far (If you want to use something else, substitute /episode/ with your choice)

wp_update_post(array('ID' => $post->ID,'post_name' => 'episode/'. get_post_meta($post->ID,'incr_number', true)));

When it's save, the post's slug is domain/episode1 and I'm trying to get /episode/1.

  • 2
    Slugs can't contain slashes. – Milo Sep 18 '17 at 17:48
  • @Milo Can you move your comment as an answer? – Gregory Schultz Sep 18 '17 at 18:13
  • do you want to do this with a custom post type or with the basic posts ? – mmm Sep 18 '17 at 18:23
  • On basic posts. – Gregory Schultz Sep 18 '17 at 18:35

Well it turns out you can't use a slash ('/') in the slug but I found a solution from How to set permalink structure via functions.php, set the permalink structure in your theme's functions.php file

add_action( 'init', function() {
global $wp_rewrite;
$wp_rewrite->set_permalink_structure( '/episode/%postname%/' );
} );

It will add /episode/ before the post slug

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.