I've created a custom post type, let's call it 'product'. The site is also bilingual, without any plugins, where I place localised pages under parent language pages, such as:

  • English
    • Page 1
    • Page 2
    • Page 3
  • French
    • Paginer 1
    • Paginer 2
    • Paginer 3

And so on. This means that my URLs are like /%language%/%pagename%, eg /en/page_1.

My custom post has bilingual fields for simple localisation but the problem is getting the correct language passed to their public page. For example the page where I list the custom posts exists in all languages and creates a URL like /%language%/parent_page/%custom_post_name%, eg /en/parent_page/a_custom_product.

The weird thing is that the links work perfectly but WordPress doesn't a rewrite of the URL so that it becomes /%post_type%/%custom_post_name%/ eg /product/a_custom_product and because we've now lost the language part I can't localise it.

I've created a function for the filter generate_rewrite_rules:

global $wp_rewrite;

$wp_rewrite->add_rewrite_tag( '%lang%', '(.+?)', 'lang=' );

$n  = array(
    '([\w]{2})/products/([^/])/?'   => 'index.php?pagename=$2&lang=$1'

return $n + $r->rules;

I've also flushed the permalinks and what not, I'm just interested to why WordPress rewrites the URL and how I can stop it becoming the former.

1 Answer 1


Oh dear, turns out I need to touch up on my regex, the second match only matches one character. It ought to be:

'([\w]{2})/the-wines/([^/]+)/?' => 'index.php?pagename=$matches[2]&lang=$matches[1]'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.