WordPress Performance Optimization Through Permalinks (Theory)
I will tell you about another type of WordPress optimization, which I am the author of. I will call it "optimization for permalinks". I use this principle on some websites where category URLs are used. In other cases, when the URL contains only the post name, post ID, date + name, or any combination of these, this optimization is not worth it. Of course, it also doesn't make sense to apply this optimization if permalinks are not enabled at all.
A Little Poetry
It was evening, there was nothing else to do because the internet had been down for the fourth week already. It was during those scarce, internet-free days, which have already passed about six months ago, that I came up with this optimization method.
Later, when I put the theory into practice, I was pleasantly surprised by the results obtained. In some rare cases, the result simply amazed me.
Permalink Theory
WordPress users are probably familiar with the function that generates permanent links, called the_permalink(). Pause for a second and think, how does it work? For those who can't think of anything, let me explain: when we call this function (usually within the WP loop), numerous operations are performed (depending on the type of permalink) to generate the URL. This function literally assembles the link, depending on the type of permalink set.
For example, if one of the basic types is set: /%year%/%monthnum%/%day%/%postname%/
, then article links look something like this: http://example.com/2010/10/27/post-name/
. Now, let's understand how the function the_permalink() creates this link: when the function is called, first the data about the type of permalink we have set is obtained, then based on the set type, the data needed to generate the link is determined, and then operations to assemble the link are carried out. In this example, the global variable $post is read, which contains the post's date and its name (slug), and based on this data a link is assembled.
This type of permalink, in general, is sufficient for the_permalink() not to create unnecessary load on the server during page generation, but it's still there. It's optimal because all this data is present in the variable $post, which the function can easily retrieve. But what if the permalink structure contains the %category%, %tag%, or %author% placeholders? In such cases, the variable $post won't contain all the data needed to create a permanent link, and additional data that is stored in the cache will be requested, for example, categories (%category%) or tags (%tag%) will be collected through taxonomies.
Getting the category in which the post is located and all the parent categories for the current one, it's fair to say that it's not the fastest process, and imagine if you have 200-300 links on a page because they are all collected in this way. This is why disabling the built-in WordPress cache increases the number of database queries to hundreds.
If we look at the basic permalink settings, we will see that there are no options where the placeholders %category%, %tag%, or %author% are used, and by default, permalinks are disabled.
By the way, here are all the placeholders that can be used in building permalinks in WordPress:
Placeholders that are immediately available and create minimal load on link generation:
- %year% - the year the post was published. For example, 2004
- %monthnum% - the month the post was published. For example, 05
- %day% - day of the month. For example, 28
- %hour% - hour when the post was published. For example, 15
- %minute% - minute. For example, 43
- %second% - second. For example, 33
- %postname% - post title, name (post slug). For example, post-title
- %post_id% - post ID (unique - never repeated). For example, 423
Placeholders that create increased load on link generation:
- %category% - category names in which the post is located. If the category has a parent category, it will also be shown. For example, main_category/post_category.
- %tag% - adapted tag name of the post. For example, post_tag
- %author% - adapted author name of the post. For example author
WordPress developers do not recommend using the last 3 placeholders.
In specific numbers, I can say that to generate a link type %category%/%postname%
with the cache turned off, 5 database queries are made. If the cache is enabled, then 0 database queries are made, and the data is retrieved from the cache. Also, it is important to remember that a series of PHP calculations are also made during this process.
I think everything said above is enough to understand how permanent links work in WordPress and what needs to be paid attention to at the moment, especially if for some reason you cannot use caching plugins or are using plugins that cache requests, but not the entire page.
What to do?
I published my own solution to this problem in the second part of the article.