How to easily use custom fields inside WordPress permalinks?

The native WordPress settings and rewrite rules does not allow to fully customize the posts & categories permalinks.

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

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.

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)

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) 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.

Go to Advanced Custom Fields dashboard and select Field Group with searched custom fields.

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 placed.

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%
Please note that the meta values will be used correctly only if the meta value is stored as a text string, therefore it is recommended to use only custom fields with “Field Type” set to “Text”!

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

If you would like to use the new permastructures for old permalinks, please use “Regenerate/reset” tool available in another plugin’s section (“Tools“).

To reset the Product’s permalinks for “Team” custom post type, please use following settings:

  • Mode” to: “Custom URIs
  • Select content type” to: “Post types
  • Select post types” to: “Team
  • Select post statuses” to “Published

After “Regenerate” button is clicked, a list of updated permalinks should be displayed in the bottom.

Add new and edit old permalinks individually

When the post is saved and custom fields are not empty, the default URI (with custom fields values inserted) will be available to use.

You can auto-update the URI (then after the post is saved, default URI will be automatically recreated and used for the permalink if the custom fields’ values are changed). It is also possible to manually change the permalinks and overwrite the default URI for particular posts.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

*