Custom Field for Singular Permalinks
-
Hi! I’m trying to customize my page permalinks in a specific way. Basically, I want to create an optional custom field on single pages. If this field has a value, WordPress should use it in place of the post slug when generating permalinks.
Say I have a page structure like this:
- Chicago
- Population
- Landmarks
- Baltimore
- Population
- Landmarks
I want the permalinks to look like this:
- /chicago/
- /chicago/population/
- /chicago/landmarks/
- /baltimore/
- /baltimore/population/
- /baltimore/landmarks/
Normally, since WordPress requires unique post slugs (which I don’t want to change), those two “Population” pages cannot both have “/population/” as the URL part. So I want to add a new field, set the value to “population” for both pages, and have WordPress pick it up and use it.
The same logic should apply to parent pages. For example, given the pages:
- Parent (slug: ‘parent-slug’, custom field: ‘parent-custom’)
- Child (slug: ‘child-slug’, custom field: ‘child-custom’)
If only the parent has the custom field, then the permalink should be: /parent-custom/child-slug
If only the child has the custom field, then the permalink should be: /parent-slug/child-custom
And if they both are customized, then: /parent-custom/child-custom
Obviously, there would also have to be some kind of error handling to make sure URLs are never duplicated. Maybe some function that is triggered by the save_post hook? I’m comfortable writing PHP and editing theme code, so if it’s a job for filters, I’m game.
(Note to mods: I posted a similar question about six months ago, but I realized it was in the wrong forum, so I’m reposting here.)
- The topic ‘Custom Field for Singular Permalinks’ is closed to new replies.