3

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?

10

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!

| improve this answer | |
  • Can you please suggest wich option would be better if I also have "custom taxonomy for this custom post type" (custom taxonomy name is 'gallery_categories', example of galery category is "auto"). And I wish myurl.com/galleries/auto would work the same as myurl.com/galleries. I wish it would have almost the same code (code would check if it is 'galleries' or 'galleries'/xxx and act in very the same way with minor differences)? – renathy Mar 1 '16 at 12:08
  • Custom post type archive and custom taxonomy archive are different. The fist option is always good but it will not work as myurl.com/galleries/auto You can not pull results from term auto. For this you will need custom page template and URL rewrite to parse the request as example.com/page-name/?mytax=auto – Sumit Mar 1 '16 at 12:15
  • 1
    What if you want to translate "galleries" :)) – trainoasis Jul 18 '18 at 15:23
  • Great. For me only worked the third option beacuse my case was about redirect the archive request to a certain post of this post type. So I added home_url( '/galleries/some-post-slug' ) inside the wp_redirect() method and so it worked. Thank you – Adriana Hernández Feb 22 '19 at 21:01
0

Here is below code to remove archive page link with not affecting in single post url.and you can create page with archive slug.

function dg_custom_post_type_args( $args, $post_type ) {
if ( $post_type === "custom post type" ) {
    $args['rewrite'] = array(
    'with_front' => false,
    'slug' => 'slug here'
  );
}

return $args;
}
add_filter( 'register_post_type_args', 'dg_custom_post_type_args', 20, 2 );
| improve this answer | |

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.