15

Sorry if this is a duplicate – I can't find an answer.

How do I rewrite a custom post type archive page to a 'man-made' WordPress page?

For example:

  • Single page: http://www.mywebsite.com/cool-post-type/awesome-single-post/
  • Archive page: http://www.mywebsite.com/cool-post-type/

I want http://www.mywebsite.com/cool-post-type/ to rewrite to http://www.mywebsite.com/about-cool-post-types/.

Can I just do this with htaccess and what would the syntax be? (I'm not good at htaccess!)

This does not work:

RewriteRule ^cool-post-type\/\?$ /about-cool-post-types [L]

Or do I have to do this in the WordPress source code? I don't want to!

3 Answers 3

18

This one's actually pretty easy. When you declare your post type using register_post_type, you need to add a new argument for 'has_archive'.

So you'll add in something to the effect of:

'has_archive' => 'about-cool-post-types'

Then, go to your Settings > Permalinks to flush them and it should work. I tested it locally, and this seems to be the way to automatically generate your archive page at a different URL. Then, you should be able to create a page at the CPT's slug.

1
  • This stopped working but I managed to fix it by assigning 'has_archive' to arbitrary text and renaming my page's slug to the category slug.
    – Warwick
    Oct 24, 2016 at 11:01
8

There's also a simple, 1-file plugin for this now, by the HumanMade folk;

https://github.com/humanmade/page-for-post-type

(Meta: I know answers with links aren't generally great, but I don't just want to copy and paste the source-code for that thing into an answer. What's the best strategy for this type of thing?)

1
  • This is a pastebin for @RichardsTape answer. A bit late but you never know. pastebin.com/RipbJ64q
    – Adam
    Apr 13 at 13:00
1

Follow up to the accepted answer.

When you declare your post type using register_post_type, you need to add a new argument for 'has_archive'.

What I currently (2023) do is the following:

'has_archive' => wp_make_link_relative(get_permalink($page_id))

where as the post id I define a ACF options field and get the id for that page like so:

'has_archive' => wp_make_link_relative(get_permalink(get_option('archive_page_id','options')))

Doing it this way, the page can easyly changed via the wordpress backend. Don't forget to re-save the permalinks in order for this to work!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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