0

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.com/author1/post-slug
example.com/author2/post-slug-2

Desired Implementation:

example.com/author1/post-slug
example.com/author2/post-slug

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

  • 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 Oct 31 '18 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 Oct 31 '18 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 Nov 1 '18 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 Nov 1 '18 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 Nov 7 '18 at 0:58
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.com/questions/10750931/wordpress-how-to-add-hierarchy-to-posts

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.