1

I have created a post named 'get started' under c-program category and permalink is: http://localhost/fresh/c-program/get-started/ and I want to create another 'get started' post under phm-program category and expected permalink is: http://localhost/fresh/php-program/get-started/ but I permalink is set as: http://localhost/fresh/php-program/get-started2/. these two links are unique. So why http://localhost/fresh/php-program/get-started/ becomes http://localhost/fresh/php-program/get-started2/?

  • Single posts are queried by slug alone, regardless of your permalink structure. – Milo Jun 5 '18 at 18:36
1

Reason for such behavior lies in wp_insert_post function. When you look at its code, on line 3319 you'll see this line:

$post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent );

And what this function does is (straight from Codex):

Computes a unique slug for the post, when given the desired slug and some post details.

And it ignores taxonomies. If the post type is hierarchical, then it allows same slugs, if posts have different parents. But if post type isn't hierarchical, it just makes sure that the slug is unique.

And it makes sense - you can reassign post to another category and that will create conflicts. Also default WP permalink structure doesn't use category as part of URL.

But... There is a way to change it (although it's a little bit risky...) At the end of that function it returns:

return apply_filters( 'wp_unique_post_slug', $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug );

So you can use wp_unique_post_slug hook and modify the computed 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.