I have a CPT with slug function (singular) and archive slug functions (plural).

Permalink structure for:

  • singular is /function/[postname]/
  • archive is /functions/[page-x/].

(can poke live at QueryPosts )

This works and looks fine, however I have logged some cases when people manipulate URL directly, like:

  • /functions/[postname] (typing name after archive page)
  • /function/ (erasing function name)

While this is not complicated to correct with some code explicitly, there would be more post types and it seems like data and logic are quite sufficient for generic solution (really I kinda expected WP to handle it, it does quite a lot to correct link issues).

However I have deep WP rewrite traumas and could use some pointers. 🙂

To sum it up:

How-To catch and redirect URLs that are correct, other than wrongly using archive slug instead of singular and vice versa?

Archive setup is registered like this:

'has_archive' => 'functions',
'rewrite'     => array(
    'feeds'      => false,
    'with_front' => false,
),

2 Answers
2

  1. Hook into '404_template'. (Example)
  2. Fetch all public custom post types where has_archive is not FALSE.
  3. Find the post type’s has_archive string and see if it is part of the current request’s url.
  4. Try get_page_by_title() with the last part of the requests.
  5. wp_redirect() to the found post’s permalink.
  6. exit;.

Leave a Reply

Your email address will not be published. Required fields are marked *