Hello,
My posts are accesible under 2 different urls: mywebsite/custom-post-slug and mywebsite/staca/custom-post-slug. So I would like to mask the second one and change the slug in the address bar with the value of a custom field like mywebsite/staca/custom-field-value. Is there a way to achieve that?
Thank you!
It sounds like you want to specify two different URLs for the same post, one using the slug and another using a custom field value. According to the WordPress documentation for Permalink Structure Tags, there isn't a built-in way to access custom field information in rewrite rules, and there's no built-in way to build a permalink that doesn't end with post ID or post slug:
https://codex.wordpress.org/Using_Permalinks#Structure_Tags
So any programmatic implementation to achieve something like this automatically would require some custom code, or possibly another 3rd-party plugin, that extends these rules. Some useful information here:
https://codex.wordpress.org/Rewrite_API/add_rewrite_tag
https://codex.wordpress.org/Function_Reference/add_permastruct
https://codex.wordpress.org/Rewrite_API/add_rewrite_rule
In a quick search I see a few posts about similar issues you might find useful:
https://wordpress.stackexchange.com/questions/12824/url-rewrite-based-on-a-custom-field-value
https://wordpress.stackexchange.com/questions/144354/add-custom-values-to-permalinks-through-custom-fields-and-posts
https://wordpress.stackexchange.com/questions/263307/how-to-use-a-custom-field-with-add-rewrite-rule
If you're interested in using a more manual approach, I think you could create a custom Page for each post using the slug you want to appear at mywebsite/custom-post-slug, and include a View in the content template or template layout for that Page that shows the desired post. The single post page at mywebsite/staca/custom-field-value requires that you set the actual post slug to match the desired custom field value.
Hello Cristian,
I didn't think for the last approach that sounds really very good. It's very easy and without coding at all. That's the way I will do it, thank you!!!