I wish that my ‘custom post type archive page’ would point to Page.
The situations is as follows:
I have Page
in WordPress with permalink http://myurl.com/galleries
. This page shows list of posts with custom post type = vmgallery
. I have custom logic for this page that is working fine.
From another view, this page works like “custom post type archive page”, because it displays all posts for given custom post type vmgallery. Currently, if user goes to http://myurl.com/vmgallery/ wordpress is loading archive page (archive.php), instead, I wish http://myurl.com/galleries
page is loaded.
How to achieve this?
You have multiple options here.
1. Define post type archive slug on post type registration
By setting 'has_archive' => 'galleries'
you can define custom post type archive slug.
Check documentation.
Then you can delete your page “galleries” then add & customize the archive-post_type.php
2. Disable default archive for post type
Disable the archive by setting 'has_archive' => false
then keep the page for the post type archive.
3. Redirect archive requests to your page
You can permanent redirect default archive request to your page.
function archive_to_custom_archive() {
if( is_post_type_archive( 'post_slug' ) ) {
wp_redirect( home_url( '/galleries/' ), 301 );
exit();
}
}
add_action( 'template_redirect', 'archive_to_custom_archive' );
I will say the first method is good!