Rewriting single post url; add_rewrite_rule problem?

I have to customize single post permalink (the structure of the permalink) if posts have a certain value in a certain custom fields.

I use the filter post_link to change permalink when needed, and this works greatly (I mean, in front-end permalink are different when they should).

Read More

First question: in your opinion I have to use other filters to cover all cases of single post permalink customization?

Then I have added specific rule using add_rewrite_rule to interpret this new kind of structure, and here I face the problem.

As this permalink structure has the ID (with 0 at the beginning in order to reach 6 digit, ex. 008467) at the end, I notice that WordPress redirects to a new url with this number – cast to an integer – repeated, for example:

from

/CATEGORYNAME/2013/04/22/POSTSLUG/008467/

to

/CATEGORYNAME/2013/04/22/POSTSLUG/008467/8467/

Do not worry about the structure before POSTSLUG, in fact the important thing is that if I remove the POSTID from the structure (008647) all works fine! For example, the following works:

/CATEGORYNAME/2013/04/22/POSTSLUG/

How is it possible?! It seems that WordPress thinks that number is something different, like pagination or something similar.

This is the rule I have added (the regexp is correct, I have tried it with an online tester):

add_rewrite_rule(  
    '(.+?)/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/(.?.+?)/([0-9]{6})(/[0-9]+)?/?$',
    'index.php?pagename=$matches[5]&page=$matches[7]',
    'top');

Related posts

Leave a Reply