There are few sites i have seen which does not use post id in their permalink structure. becuase of this their url becomes.,


so what happens when user adds new post with same title or slug ?

how does wordpress knows which post to show when both posts are having same url slug ?

which post gets shown when user goes to the link

  • 1
    If the link contains something that can ever be changed, it's not permanent... what I typically do, which is what Amazon and others do, is include a unique ID for the page in the URL, followed by whatever slug, etc. you might want to include. You can either ignore what's after the ID or redirect to the proper/current link (if you want the final URL users see to reflect any changes to the slug that might've happened since the link they have was generated). – kungphu Jun 15 '16 at 7:35

Extending @Howdy_McGee answer.

  1. First you can not have two post with the same slug (Not title) which explained already in other answers.
  2. Second you can have page and post with same slug. But you will be able to access only page when your permalink structure is /%postname%/. And there is debate going on this ticket#13459 about this issue/feature since 6 years :P.

When a request is made WordPress extract the request URI and match with the permalink structure in function parse_request(). If a match is found then query_vars are filled and this information is passed to WP_Query class where actual SQL is prepared and perform to display the result.

In case of page If it is page WordPress uses get_page_by_path and retrieve the ID of page to use in SQL query.

In case of post If it is a post then WordPress uses slug of post in SQL.

In short WordPress does not required post ID (Not in page) when permalink structure is set to /%postname%/ it uses the post slug to query database.

Therefore, with same slug of post/page a page is displayed first because with two matches WP check for page request first.

|improve this answer|||||

As far as I know, you can't have two posts with the same slug. Whenever you attempt to change a post slug to something that already exists, WordPress will append -2 to the newest slug to differentiate the two. So, if you already have a post with the slug of test and attempt to create another post with the same slug, WordPress will instead give that slug test-2.

As far as how WordPress knows which post to get... During load, WordPress uses the slug and runs the wp() function to call a WP_Query() on the requested slug. If it exists then it populates the global $post and shows content, otherwise it redirects to 404.

That's my understanding on how WordPress loads. Maybe someone else has a better, more in-depth understanding.

|improve this answer|||||

Whenever a post is saved WP calls the wp_unique_post_slug function that will compute a unique slug, based on the post title and a suffix like -2 if another post has the same title.

However, you can filter this function, to generate your own slug. So, if you want to mess things up, that's possible.

|improve this answer|||||

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.