Turn off %postname% auto-incrementing?

Hi @Thompson:

Unfortunately the post name must be unique for a given post type, and hierarchy level if the post type is hierarchical.

There are a couple ways to address this:

  1. Use /%post_id%-%postname%/ instead of /%post_id%/%postname%/; that makes it unique and thus won’t append any annoying -Ns to the end of your URLs and will give you a slight improvement in SEO since the important keywords will be in the website root and not one directory level down. Or

  2. If you must have the URL structure you specify then you can just set your permalink to /%post_id%/ and use the 'post_link' and 'init' hooks to allow you to respectively append the post name onto the URL and to add a permastruct that matches a post_id, a slash, and anything after the slash but throws the latter two away because they are not used with the permalink structure:

add_filter('post_link', 'mysite_post_link',10,2);
function mysite_post_link($permalink,$post) {
  $post = get_post($post);
  return "{$permalink}{$post->post_name}/";
}
add_action('init', 'mysite_init');
function mysite_init() {
  global $wp_rewrite;
  $wp_rewrite->add_permastruct("user_submitted_post",
    '%post_id%/.*?', 
    'p=matches[1]');
  $wp_rewrite->flush_rules();  // This line is only needed once
}

Leave a Comment