How WordPress Stores Permalinks and Slugs in the Database

You will find plenty of definitions for permalinks across the web, but understanding how WordPress generates them dynamically is another story.

WordPress provides basic permalink customization options in the admin dashboard, especially for standard content types. For more tailored options, though, you may want to use a plugin like Permalink Manager. It gives you the ability to simplify URL structures, duplicate or remove slugs, and customize permalinks for each post, page, or taxonomy.

If you are new to WordPress or unsure what permalinks are, the linked article is a great starting point for some background information.

The final URL that visitors see is actually assembled from two components – individual slugs and site-wide permalink patterns. Let's dive into WordPress's database architecture to understand exactly how these pieces fit together.

Where Are Slug Saved in the WordPress Database

At its core, a permalink relies on a slug to function. The heart of every permalink is its slug. This is what makes each post, page, or taxonomy item in WordPress distinct.

For example, if you create a post titled "My Summer Vacation", WordPress would automatically generate the slug "my-summer-vacation" based on the title (unless you manually edit it).

Permalink and slug
The slug, which appears at the end of the permalink, is created by shortening the title into a more condensed, URL-friendly text.

How to Access the Slugs using MySQL

Slugs for posts, pages, and custom post types are saved in different places depending on the content type. For these specific items, you can find their slug in the "post_name" column of the wp_posts table.

To access them, you can use a straightforward SQL query like this one. If you want to access the slugs of a specific post type items, simply replace 'post' with the name of your custom post type (e.g. 'product' for WooCommerce products).

SELECT post_name FROM wp_posts
WHERE post_type = 'post';

Posts slugs in DB

Slugs are also used for taxonomies like categories, tags, and custom taxonomies. These slugs are stored in the wp_terms table in the "slug" column. If you want to fetch slugs for a taxonomy like "product_category," here is the SQL query you will need:

SELECT slug FROM wp_terms AS t LEFT JOIN wp_term_taxonomy AS tt ON t.term_id = tt.term_i
WHERE tt.taxonomy = 'product_category'

Terms slugs in DB

To change the base URL for a custom post type or taxonomy, you can use the "rewrite slug" argument. It will define the permalink base that appears directly after your domain, before the slug of individual post/term.

WordPress sets default slugs for built-in posts, pages, categories, and tags. These base slugs can be modified to some extent through the admin dashboard, allowing you to easily adjust the structure of URLs for your built-in content types.

Post, Pages, Categories and Tags

The general permalink structure is defined in the WordPress admin area under "Settings > Permalinks". These settings allow you to adjust how URLs for built-in content types such as posts and pages are formatted.

Changing permalink in admin dashboard

Custom Post Types & Taxonomies

Custom post types and taxonomies, however, need a unique rewrite slug, which is used as the base for their URL structure. For instance, if you declare a custom post type called “Events”, the URL might look like:

https://example.com/events/event-name/

This "events" rewrite slug is declared in your theme or plugin code via either the register_post_type() or register_taxonomy() function.

Rewrite slug

If you omit the rewrite argument entirely when registering a custom post type or taxonomy, WordPress will use its default value. This means that WordPress will automatically use a URL structure based on the content type's name (e.g "events" for Events).

Rewrite settings for taxonomy in ACF
When registering the content types with ACF you can set the custom permalink base using "URL Slug" under Advanced Settings.

When a visitors requests a URL, WordPress does not just pull a permalink from the database, because it does not store complete URLs. Instead, it uses a more efficient process, interpreting the URL with internal rewrite rules to match it to the right content.

Rewrite rules

Rewrite rules are patterns that WordPress uses to map URLs to specific content types (posts, pages, taxonomies, etc.). These rules are essentially instructions that tell WordPress how to interpret various parts of a URL and which query to execute based on the structure of that URL.

Rewrite rules list

In WordPress, rewrite rules are made up of URL patterns. These rules help keep the database from being overloaded with URLs by storing just the patterns and slugs for posts and terms. This way, WordPress can still handle URLs correctly, even if you change the permalink settings.

As mentioned earlier, the rewrite rules depend on your permalink settings in WordPress, as well as any custom post types and taxonomies you have registered. These rules are dynamic and will change if you modify the permalink settings.

Matching Slugs to Content - Query Parsing

Once the rewrite rules are in place, WordPress uses them to parse the URL components and map them to the corresponding query that fetches the respective content item. For example, when a user requests:

https://example.com/events/summer-vacation/

WordPress uses its rewrite rules to determine that the URL corresponds to a event item with the slug "my-summer-vacation".

Keep in mind that this approach come with a few limitations. Since it is based on pattern matching, it works well with simple structures but may not handle more complex ones.

For instance, it does not allow to reuse the same permalink base for different content types.

If you find WordPress's default permalink system limiting, using a plugin like Permalink Manager an help you take full control of your URL structure:

  • Remove or Duplicate Rewrite Slugs:
    By default, WordPress adds a base slug for custom post types and taxonomies. With Permalink Manager, you can remove this base slug entirely, resulting in cleaner and more tailored URLs (e.g., example.com/product-name/).
  • Create Custom URL Structures:
    While WordPress allows for some customization in the settings, Permalink Manager gives you the freedom to use any URL structure you need, without limitations. You can also adjust the individual permalinks for posts, pages, and custom post types' & taxonomies' items.
by Maciej Bis

Leave a Reply

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