admin管理员组

文章数量:1430482

From what I can tell, wp_insert_post will automatically append a -# to your post slug. So why does wp_unique_post_slug exist?

Is it just there for legacy purposes or is there still a good reason to use it?

From what I can tell, wp_insert_post will automatically append a -# to your post slug. So why does wp_unique_post_slug exist?

Is it just there for legacy purposes or is there still a good reason to use it?

Share Improve this question edited May 21, 2019 at 9:03 cjbj 15k16 gold badges42 silver badges89 bronze badges asked May 21, 2019 at 3:15 Pikamander2Pikamander2 6287 silver badges20 bronze badges 2
  • if you scroll down in in the source code of wp_insert_post() developer.wordpress/reference/functions/wp_insert_post/… to line 3679, you will see that that function actually calls wp_unique_post_slug() ... – Michael Commented May 21, 2019 at 3:43
  • 2 It exists because it's the one which automatically adds that -# (i.e. number suffix) to the post slug when there's an existing post with the same slug without the suffix. So the function exists and being called from wp_insert_post() to make sure the slug is unique - just as the function name implies. – Sally CJ Commented May 21, 2019 at 4:01
Add a comment  | 

1 Answer 1

Reset to default 2

As the commenters have noticed wp_unique_post_slug is called from wp_insert_post to ensure there are no double slugs. It is also called from two other functions, which explains why it is a separate function and not incorporated in wp_insert_post.

A little used feature is the possibility to apply filters present in wp_unique_post_slug. There are two of them:

  1. pre_wp_unique_post_slug allows you to completely bypass the unique post slug generation, for instance if you want to use a completely different slug for your posts (say, based on some metafield in stead of the title).
  2. wp_unique_post_slug allows you to change the unique post slug that has been generated, for instance if you dislike the number-suffix and want to replace it with something else or if you want to prepend every slug with the post tag or so.

So, apart from the obvious fact that wp_unique_post_slug is an essential WP function, there are also advanced uses. 

本文标签: postsIs there any point to using wpuniquepostslug