$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>permalinks - How to allow different authors to use same post slug?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

permalinks - How to allow different authors to use same post slug?

matteradmin9PV0评论

We have a multi-author WordPress site where different authors need the ability to use the same slug (post_name). Right now WordPress will append a -2 to the slug but how can we prevent WordPress from appending dashes and keeping the slug the same? Note that our permalinks will still be unique even if the slug is the same as we use the author's username in the permalink as illustrated below:

Current Implementation:

example/author1/post-slug
example/author2/post-slug-2

Desired Implementation:

example/author1/post-slug
example/author2/post-slug

Please advise on how we can achieve the desired implementation. Thank you.

We have a multi-author WordPress site where different authors need the ability to use the same slug (post_name). Right now WordPress will append a -2 to the slug but how can we prevent WordPress from appending dashes and keeping the slug the same? Note that our permalinks will still be unique even if the slug is the same as we use the author's username in the permalink as illustrated below:

Current Implementation:

example/author1/post-slug
example/author2/post-slug-2

Desired Implementation:

example/author1/post-slug
example/author2/post-slug

Please advise on how we can achieve the desired implementation. Thank you.

Share Improve this question asked Oct 31, 2018 at 20:09 HBCondoHBCondo 1537 bronze badges 5
  • It sounds like you're using a non-hierarchical post type. As long as you are using Pages, or a CPT such as perhaps "author" that is hierarchical, WP will allow duplicate child slugs as long as the top-level pages (author1, author2) have unique slugs. – WebElaine Commented Oct 31, 2018 at 21:18
  • Thank you for your reply. This is indeed for non-hierarchical post types but these are for standard WordPress posts (not custom), not pages. – HBCondo Commented Oct 31, 2018 at 22:52
  • That's your problem - you cannot have duplicate slugs in a non-hierarchical post type like Post. WP looks at the slug first to determine what content to display, and if there's more than one - and they don't have a parent - it would not know which to display. To get URLs like this you will have to use a hierarchical post type. – WebElaine Commented Nov 1, 2018 at 14:31
  • I'd say assign a custom field to the posts, maybe name it author_post_slug and give it the same value (e.g. post_slug), and filter the URL requests via parse_request, or create custom rewrite rules for those URLs. – Sally CJ Commented Nov 1, 2018 at 19:42
  • Thank you for these suggestions. We already imported over 200K posts using the standard post type. We may just specify the author id in the post_parent field so we can achieve having the same post_name for different authors. Thoughts on this approach? Adding custom fields for our large post count may not be feasible and rewriting doesn't really solve the problem of WP appending -2 to post_name. – HBCondo Commented Nov 7, 2018 at 0:58
Add a comment  | 

1 Answer 1

Reset to default 0

I ended up adding this code that makes the default post type hierarchical and populating the wp_posts.post_parent field with the author ID. The combination of these two has achieved the desired implementation of having the same slug across multiple authors.

// Set post type "post" to be hierarchical
$wp_post_types['post']->hierarchical = 1;

Source: https://stackoverflow/questions/10750931/wordpress-how-to-add-hierarchy-to-posts

Post a comment

comment list (0)

  1. No comments so far