3

I run a news site and there's a Wordpress permalink issue that I have been wondering. Now, Wordpress will add an incremental numeric suffix to duplicate %postname% regardless of the directory structure.

For example, wordpress will add a "-2" suffix to the permalink regardless if the structure is domain.com/2016/04/03/news-title or domain.com/category/news-title-%post_id% when it detects a duplicate.

I initially believe that Wordpress would not add the suffix automatically after appending a %post-id% to the end of the permalink but that is not the case.

What I'm trying to achieve (without Wordpress meddling with the %postname%) is the following:

  • domain.com/2016/04/03/news-title-44956 (assuming the numbers behind is the %post_id)
  • domain.com/2017/05/06/news-title-55957

or

  • domain.com/2016/04/03/44956/news-title
  • domain.com/2017/05/06/55957/news-title

I hate the idea that I won't be able to use the same title with a different content a year later (or many years later) even though the permalink directory is vastly different by then.

Is there any way I can add hooks or temporarily modifying the Wordpress core to fix this or at least get Wordpress to automatically append the %post-id% instead of an incremental number when it detects a duplicate permalink?

1 Answer 1

1

I suppose you could filter wp_unique_post_slug and have it return the original slug for posts, but I'm not sure what kind of side-effects this may have:

add_filter( 'wp_unique_post_slug', function( $slug, $post_id, $post_status, $post_type, $post_parent, $original_slug ) {
    if ( $post_type == 'post' )
        $slug = $original_slug;

    return $slug;
}, 10, 6 );

And here's how you would append the post ID instead of -2:

add_filter( 'wp_unique_post_slug', function( $slug, $post_id, $post_status, $post_type, $post_parent, $original_slug ) {
    if ( $post_type == 'post' && $slug != $original_slug )
        $slug = preg_replace( '#\-[0-9]+$#', '-' . $post_id, $slug );

    return $slug;
}, 10, 6 );
0

Your Answer

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

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