Custom Permalinks for hierarchical taxonomies

The title is pretty vast and non-descriptive, but it sets the general idea of the post. In general the Permalinks system that WordPress uses is pretty flexible, but it also has it’s limitations. Quite recently as I was working on a client’s project, I stumbled upon one of them.

The project was to create a Frequently Asked Questions database, using WordPress. The client wanted the following structure for the database(this is a simplified version)::

  • Index of the database ( ) [Index of the whole database]
    • Main Category ( ) [Index of the Main Category]
      • Post in the Main Category ( ) [View only Post 1]
      • Sub-Category ( ) [Index of the Sub-Category]
        • Post in the Sub-Category ( ) [View only Post 2]
    • Another Main Category ( ) [Index of the other Main Category=]

Everything looks normal(as structure of the URL’s) for the regular user, but WordPress has a different idea on the matter Instead of an URL like we will be able to see the index of the Main Category at an URL like, which as you can notice is quite different from the initial idea. To fix this issue we have to add a couple of custom Rewrite Rules and functions.

The first step is to register the new Custom Post Type and it’s Custom Taxonomy. Here’s how:

This code registers the new post type and it’s “FAQ Categories” taxonomy, which we will use to categorize the questions.

After we register the new post type, we have to add two functions that will make the Permalinks work the way we want them to:

In the code above we registered two functions – register_faq_rewrite_rules() and fix_faq_subcategory_query().

The first function adds the following rewrite rules:

  • faq/([^/]+)/?$ – this rule will be applied when the URL of the page that is being loaded looks something like “faq/any-character/” – in other words when the URL starts(after the part with the home page URL) with “faq/” followed by any combination of symbols, without “/”( “([^/]+)” ), and possibly followed by a “/”( “/?” ). Also in order to match, this part has to be the last part of the URL(in other words there won’t be a match, if the URL is “faq/any-character/something”).When the current URL matches, the page will display a FAQ Category archive for the category(according to the example I gave) “any-character”.
  • faq/([^/]+)/([^/]+)/?$ – this rule is almost the same as the previous, except for the URL that would match this rule looks like “faq/any-character/post-slug/”.This will display a post with slug “post-slug” from a FAQ Category with slug “any-character” OR an FAQ archive for a FAQ category with a slug “post-slug” that is child of the “any-character” FAQ category.
  • faq/([^/]+)/([^/]+)/page/(\d{1,})/?$ – this rule will match paginated results for a sub-category archive.
  • faq/([^/]+)/([^/]+)/([^/]+)/?$ – this rule is almost the same as the previous, except for the URL that would match this rule looks like “faq/any-character/sub-category/post-slug/”.This will display a post with slug “post-slug” from a FAQ Category with slug “sub-category”.

The second function on the other hand fixes the issue with the sub-categories. The issue itself consists in the fact, that when you try to load a page with URL faq/category/child-category/, WordPress would try to load a post with slug “child-category” instead of the sub-category with slug “child-category”. The function itself is probably not the best solution to the problem, at least because we have to make an additional query to the database, but it’s the only solution I was able to come-up with Since the function checks if there are posts with slug “child-category”, if you happen to have posts(from the FAQ post type) and categories with the same slug, you might get unexpected results

That’s enough to make the FAQ database run properly, but right now if you want to fully use the new Rewrite rules, you’d have to write every URL manually(I mean, when you link to content from the FAQ database). This is because WordPress doesn’t know how to generate the links, so that they match our idea. That’s why we add a couple of more functions:

The above functions “filter” the output from two built-in WordPress functions, which take care of creating the correct URL’s for all pages, posts, taxonomy archives, etc.

There is one more issue, that could be a serious problem for some users – the duplicate content issue. There is a rather large chance, that Google or another search engine that crawls your website to get quite upset by the fact that it can find identical content in two different URL’s(for instance faq-item/post-2-slug/ and faq/category/sub-category/post-2-slug/). This can be avoided with a function that redirects the users to the correct address, but I’ll leave this up to you

If you want to see the whole thing in action, you can go to Custom Permalinks Test.

2 Moons

The 2 Moons

People have been using the phases of the Moon to keep track on time for many centuries. I don’t think it’s really common nowadays though In this post I’ll be generally referring to a “Moon” as one full lunar cycle(or approximately 29.5 days)

2 Moons is not a small amount of time, considering the amount of events(including life-changing ones, both globally and personally) that can(and DO) occur in this period. I feel I’m writing nonsense right now, so I’ll move on

About 2 Moons ago, me and my Love laid the foundations of a really strong spiritual(or sensuous) bond. None of us had any idea what it would turn into subsequently. We both kind of jumped into a relationship that wouldn’t make sense to most people(the circumstances were a bit complicated by that time). We just gave it a try and let our feelings run wild.

That was a good decision, maybe perhaps the best choice we’ve ever made. We got to know a lot about each other from the whole beginning of this relationship. We spent about 2 thirds or maybe even more of our first Moon living together – some of the time at her place(at that time), some of it at my place. We got really attached to each other, so attached in fact that at the end of our first Moon, we could barely spend time apart from each other.

She had plans for her near(and not so near ) future and I had none. That made it pretty easy for me to fit into her plans. She was moving to Istanbul at the beginning of September to continue her education in the best university in Turkey. So initially my idea was to spend half of the time here in Varna and half of it in Istanbul. But as I mentioned before by the end of the first Moon together, we could barely stay away from each other.

This required a change in my plans and luckily I’ve arranged my life in such a way that I can work from any place in the world as long as it has a good internet connection available. This gives me the freedom to change my location really really easy. So I decided to move in Istanbul with her. I googled for WordPress jobs in Istanbul and sent-out some emails to a couple of companies that were hiring WordPress experts. One of them(C2A) replied and we started working remotely together. Then when I went to Istanbul I worked a bit from their office and they were happy with my work and decided to hire me

Now we even have our own place in Istanbul – not really big, maybe not perfect, but cosy and ours.

It really amazes me how quickly one’s life can change completely. I’m ok with changes though(a friend of mine used to say that if someone that hasn’t seen you in a long time tells you that you’re the same, then there is something wrong with you). I think that they are an essential part of life – with every second we change more or less – and we should learn to accept changes(of course there are changes that should not just be accepted the way they are, but that’s a different topic).

I still don’t know if I believe in signs and whether things happen for a reason or not, but this whole thing seemed to be full of signs – I’m not going to share them here, but I definitely think there were signs indeed

I feel happy and complete. I feel absolutely lucky for having such an amazing(I don’t think there is a word strong enough to describe her, but this one works as well) girl.

Thank you Love, for sharing your life and love with me!

PP: Sorry for the lack of order in my thoughts, but that’s how things went out of my head – I’m not going to reorder them now that they’re out