0

I have a custom post type with a rewrite rule containing a variable, which is later replaced by the a category term 'smoothie_category'. The argument line passed to register_post_type is:

'rewrite' => ['slug' => 'smoothies/%smoothie_category%'],

An example URL:

/smoothies/category-slug/smoothie-slug

I am using the post_type_link hook to replace this with the post's category term - everything is working correctly thus far.

The issue I have is that the URL can be manipulated to use any string in place of the real category:

/smoothies/any-string-here/smoothie-slug

Whilst it reaches the correct content, I want to be able to 301 redirect to the correct permalink which I believe is what blog posts do - I have the post category as part of the URL: /blog/category/post-slug, and using an invalid category slug causes a 301 to the correct post permalink, which is the behaviour I'd like to replicate.

Whilst I can add code in my template, what is the correct, or suggested hook to use to intercept invalid URLs and redirect them to my custom post permalink?

  • Because invalid URL etc is being handled and redirect to 404 template if not found, you may use template_include filter which is the last exit door after 404 is set, any URL is being parsed at query first, so query will be the first entrance when URL is being parsed which you could use request filter for pre-handling. Hope it helps. – simongcc Apr 13 at 9:53

Your Answer

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

Browse other questions tagged or ask your own question.