0

I am building a magazine website with an issue custom post type. The slugs for each issue follow the format YYYY-MM-DD according to the issue's publish date. Currently the permalinks are set up as http://host/magazine/2014-06-08 but I would like them to be accessed through the URL http://host/magazine/2014/06/08.

Here is what I'm currently doing in my functions.php:

$issue_structure = '/magazine/%issue%';
$wp_rewrite->add_rewrite_tag("%issue%", '(\d{4}-\d{2}-\d{2})', "issue=");
$wp_rewrite->add_permastruct('issue', $issue_structure, false);

I tried adding this to my .htaccess file:
RewriteRule ^magazine/(\d{4})/(\d{2})/(\d{2}) /magazine/$1-$2-$3 [QSA,L]

And I also tried added this to my WP_Rewrite rules:
add_rewrite_rule('magazine/(\d{4})/(\d{2})/(\d{2})', 'magazine/$matches[1]-$matches[2]-$matches[3]', 'top');

Neither of those approaches have worked, either separately or in combination. And I was careful to flush my rewrite rules. In fact, there's no evidence of that add_rewrite_rule call in my Rewrite Rules Inspector list, so I suspect I'm not using it correctly.

Another (hacky) thing I tried was manually tweaking the post_name field in the database to be 2014/06/08 and switching up the rewrite tag to be $wp_rewrite->add_rewrite_tag("%issue%", '(\d{4}/\d{2}/\d{2})', "issue="); but that didn't work either.

What else should I try?

1 Answer 1

1

Internal rewrite rules have to point to index.php and set the proper query vars for WordPress to be able to load the requested object:

add_rewrite_rule(
    'magazine/(\d{4})/(\d{2})/(\d{2})/?',
    'index.php?issue=$matches[1]-$matches[2]-$matches[3]',
    'top'
);

That should get incoming requests to resolve correctly, however, that's only half the task. If you call the_permalink for your posts, you'll still get the format you defined when you registered your post type. To fix that you have to filter post_type_link to output the URLs in the correct format.

0

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.