Skip Navigation

[Resolved] How to Mask Post Slug in Url with the Value of a Custom Field

This support ticket is created 6 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by ioannisM-2 6 years, 6 months ago.

Assisted by: Christian Cox.

Author
Posts
#583025

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!

#583119

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.

#586187

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!!!

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.