Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I use the permalink structure /%post_id%/%postname% because I have user submitted content. The problem is that %postname% still auto-increments itself and looks funny, even though the post id makes it unique. Is there a way to disable %postname% auto-incrementing?

domain.com/634/apple-pie
domain.com/635/apple-pie-2
domain.com/636/apple-pie-3

should be:

domain.com/634/apple-pie
domain.com/635/apple-pie
domain.com/636/apple-pie

Bonus points if you can do this just for a specified custom post type.

share|improve this question

2 Answers 2

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
}
share|improve this answer
    
Hey Mike Thanks. I'll give #2 a shot. Looks pretty good. I still think if I dig deep in the core that there has to be a way to turn off -n. If WP can create it, there has to be a way to not create it. But I'll give yours a go first. BTW, I really enjoyed your WordPress v Drupal post! –  Thompson Jan 28 '11 at 4:28
    
@Thompson - What makes you think that? I know that part of core like the back of my hand and know where these things lie. Yes I can show you how to save the record so it doesn't do that but it will trigger other issues when WordPress is expecting the post slugs to be unique and they in fact are not. Why fight it? #2 gets you what you asked for, and #1 gives you an SEO advantage. Is there a reason why you need "/" vs. "-"? –  MikeSchinkel Jan 28 '11 at 5:01
    
@Thompson - And thanks RE: WordPress vs. Drupal. –  MikeSchinkel Jan 28 '11 at 5:01
    
I decided on "/" versus "-" because it offers the ability to use it as a short url for Twitter domain.com/recipes/post-id without using a shortener service. And if the url gets truncated by a user in emails or elsewhere, domain.com/post-id/truncated-whatev... should still work for the user. I'm not worried about seo from the url depth anymore, at least not one more directory. I read on the Google blog at one point that depth doesn't matter to Goog, as least. A page is a page to them. Wish I could find that for reference. Really, mostly for the usability reasons above. –  Thompson Jan 28 '11 at 8:36
1  
@Thompson - Okay, you are going to have to store your post names as '823-ball' and then strip off the '823-' part. I can write something to show you how, but you've never answered the question: Why can't you use a URL permastruct of '/%post_id%-%post_name%/'; why must you have it be '/%post_id%/%post_name%/'? Let me know why the URL must be that way I'll show you how. –  MikeSchinkel Jan 28 '11 at 9:44

Try this:

add_filter('wp_insert_post_data', 'remove_slug_suffix');

function remove_slug_suffix($data) {

    if ('post' == $data['post_type'])
        $data['post_name'] = preg_replace('/-\d+$/', '', $data['post_name']);

    return $data;
}
share|improve this answer
    
No luck. Doesn't seem to work for me on my CPT or regular posts. I added the code my functions.php and then tested by added two new posts with the same title and it still incremented it and wouldn't let me edit the url to manually remove the -2 either. –  Thompson Jan 27 '11 at 20:59
    
:( It will show with suffix in interface but it should be stripped when inserting post in database. Worked for couple of quick tests for me. Anyway Mike's answer gives more robust advice. –  Rarst Jan 28 '11 at 6:05
    
Thank you though! –  Thompson Jan 28 '11 at 9:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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