1. Home
  2. Docs
  3. Tutorials
  4. How to use custom fields inside WordPress permalinks?

How to use custom fields inside WordPress permalinks?

The native WordPress settings and rewrite rules does not allow to fully customize the posts & categories permalinks. Although, custom fields values can be inserted to permalinks with some tailored hooks & hacks, it is still a quite complicated operation. Permalink Manager is currently the only actively supported plugin that allows to make this process considerably easier and faster.

Some of the functionalities presented in this post are available only in Permalink Manager Pro.
Buy the plugin here.

Case study

Let’s assume that you want to add country, city and department names to our team members single pages’ permalinks to suggest where each of our employees work. Using custom taxonomies may be overkill in this case and setting this data via custom fields seems more reasonable.

Please note that the this tutorial shows only usage example and custom post type name and/or custom fields (meta tags) may be completely different in your particular case.

Step 1A. Find the “generic” custom fields (meta keys) names

If you are not using “Advanced Custom Fields” plugin to handle your custom fields, the meta keys should be listed in “Custom Fields” metabox that can be enabled with “Screen Options“.

When “Custom Fields” section is activated, you will need to scroll down the page a bit. The table with meta keys should be displayed somewhere below content editor.

The permastructure tags (displayed in “Name” column) are as follows:

  • country for “Country”, ergo permastructure tag should be: %__country%
  • city for “City”, ergo permastructure tag should be: %__city%
  • department for “Department”, ergo permastructure tag should be: %__department%

Step 1B. Find the custom fields (meta keys) names handled by Advanced Custom Fields

If your custom fields are set-up with “Advanced Custom Fields” plugin, to find the custom field name, you will need to go to ACF’s dashboard (“Custom Fields” section in the side navigation) and afterwards click on “Field Group” where your custom fields are set.

After the Field Group name is clicked, you will be redirected to “Edit Field Group” page where a table with custom fields list should be displayed.

Go to Advanced Custom Fields dashboard and select Field Group with searched custom fields.
Meta keys will be displayed in “Field Name” column.

Similarly, the meta keys (custom fields’ names) are in this case as follows:

  • country for “Country”, ergo permastructure tag should be: %__country%
  • city for “City”, ergo permastructure tag should be: %__city%
  • department for “Department”, ergo permastructure tag should be: %__department%

Supported ACF field types:

  • Text field
  • Relationship field
  • Taxonomy field
  • Select field
  • Post object field (since 2.1.1 version)

If “Relationship”, “Post object” or “Taxonomy” is used as a custom field tag, Permalink Manager will use the slug of selected post/term item.

Step 2. Adjust permastructures

After the meta keys are founded, you can insert them as tags inside the permastructures for “Team” custom post type. The settings can be altered in “Permastructures” section of the Permalink Manager’s dashboard (“Tools -> Permalink Manager -> Permastructures“).

Permastructure settings for “Team” custom post type.
The format of permastructure tags should be: %__custom_field_key%.
Please note that the updated permastructure will be automatically used only to new single pages/posts permalinks. To apply the changes to the old permalinks as well, please follow the instructions in the next step.

Step 3. Reset old permalinks

After the custom field tags are added to permastructure settings, the new permalink format will be immediately applied to all affected posts and terms. If you would like to do the same also for posts and terms that were added before, you will need to regenerate them using “Regenerate/reset” tool available in “Tools -> Permalink Manger -> Tools” section. Full instructions on how to reset them are posted here.

Update the permalink when the custom field value is changed

When the post is saved and custom fields are not empty, the default URI (with custom fields values inserted) will be used as a custom permalink.

If you would like to automatically update the actual permalink after the post is saved and the custom field value is changed, you will need to enable “Auto-update the URI” mode. You can activate it manually for each post or set globally for all posts using Permalink Manager settings (here you can find more info on how to enable this).

Of course, it is also possible to manually change the permalinks and overwrite the default URI for particular posts.

Was this article helpful to you? Yes No

How can we help?