In this gist, I'm sharing a method to set up identical permalinks for different types of objects in WordPress, such as posts, pages, and custom post types. By default, WordPress doesn't allow the same permalink structure for different objects. And even if you try to do this with add_rewrite_rule
would get confliction and one of then would actually work. I provide a solution by leveraging add_rewrite_rule
with pre_get_posts
filter.
- First, you define a custom query param using the
query_vars
filter. - Then, you set up rewrite rules using the
add_rewrite_rule
filter to map the desired permalink structure to the custom query variable. This allows WordPress to differentiate between different objects while maintaining the same permalink. - Finally, you'd need to hook into the
pre_get_posts
filter to change the main query based on the slug found in the query parameter.
In conclusion, you'd get uniform permalinks for different WordPress objects, enhancing the flexibility and customization of the platform.
If you get into trouble, you can read this article for more details