0

I have a custom post type with a URL permalink rewrite that seems to work if it uses /mynews/stories/YYYY/mm/story-name but if I change "mm" to the first 3 letters of the month (eg Jan, Feb, Mar, etc), the template hierarchy breaks and it uses index.php.

My code is here http://pastebin.com/RS1DMbB6

I put this all in the functions.php file as I'm not allowed to add plugins.

So the moment I change from %monthnum% to %month%, the URL rewrite works, but the wrong template is being used. Debugging inside the post_type_link hook and template_include tells me is_single is null, and the template seems to go to index.php instead of single-story.php or even single.php.

Any ideas why this seemingly simple change breaks template hierarchy?

  • Please update your question. Include the minimal code we need to reproduce the problem. – fuxia Mar 6 '14 at 1:21
  • Apologies. This is my very first post on StackExchange. The URLs I tried are /mynews/stories/2014/Mar/test1 where "test1" is the name of the post. This seems to make it use index.php instead of single.php. URL /mynews/stories/2014/03/test1 works as expected. – Chris Mar 6 '14 at 3:20
0

The issue is that %month% isn't a recognized rewrite tag. If you add the tag within your init action and flush permalinks, the query will succeed.

function register_story_post_type(){
    // your post type registration stuff here, and then…
    add_rewrite_tag( '%month%','([^&]+)' );
}
add_action('init','register_story_post_type');

There are also about a hundred ways you could otherwise clean up that code, but none relevant to your issue here. One thing I recommend though is to not manipulate rewrite globals directly, this is a bad habit that can get you in trouble if the underlying code changes. Always use the API and avoid globals. Your story rewrite tag is added when you register your post type, and the rewrite structure can be defined directly within your post type registration code using the rewrite argument.

  • Thanks Milo, I will update the globals. In it's current state the URL does rewrite correctly, but the template hierarchy is ignored and custom post type falls back to index.php for its template. – Chris Mar 6 '14 at 5:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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