Specify a Page as the parent to the CPT Archive

I have an “About Us” page on my site with some child pages. I have created a “History” custom post type, where the rewrite slug is ‘history’.

I use archive-cpt_history.php as the template file for that CPT. Everything works exactly as its configured, I can go to website.com/history/ to see my CPT loop in all its glory.

Read More

The issue is, I need this history CPT to be a child of About Us. How can I rewrite the slug to be /about-us/history so that it works with the archive / landing page? I can do this:

'rewrite' => array( 'slug' => '/about-us/history', 'with_front' => false ),

But that only works with the single posts, not the archive / landing page. Any help would be greatly appreciated.

Also, in case this is relevant, once I have solved this issue single-cpt_history.php will simply use ” header( ‘Location: ‘ . site_url(”) ); ” to redirect users back home. There is never any permalink to the individual History post. The design just doesn’t work like that. The History landing page will always show all posts in the CPT.

Related posts

Leave a Reply

1 comment

  1. In your register post type arguments, set rewrite to true and has_archive to the path you want for the archive page (without leading slash):

    'rewrite' => true,
    'has_archive' => 'about-us/history'
    

    As for the redirection, rather than getting all the way to the template, you could short-circuit the loading of single posts earlier and remove the need for the single cpt template:

    function wpa_parse_query( $query ){
        if( is_singular() && isset( $query->query_vars['cpt_history'] ) ){
            wp_redirect( home_url() );
        }
    }
    add_action( 'parse_query', 'wpa_parse_query' );