Let’s say I create a custom post type called ‘new projects’, with capability type = page and slug = ‘projects’.

Let’s then say that I create a new page of type ‘new projects’ and name it ‘my new project’. By default the URL would be represented as:

mysite.com/projects/my-new-project/

However, if I want the page my-new-project to be the default page (like an index) for that custom post type, is that possible? In other words, I would like mysite.com/projects/ to load the my-new-project page.

If this is possible, could I then add more pages of type ‘new projects’ and still define one of those pages as the ‘default’ page?

Thanks so much,
Tathiana

3 Answers
3

You can kind of fake it before the main query is run with the pre_get_posts action:

function wpa84126_single_project_archive( $query ){
    if( ! is_admin()
        && $query->is_main_query()
        && $query->is_post_type_archive( 'new_projects' ) )
            $query->set( 'name', 'my-new-project' );
}
add_action( 'pre_get_posts', 'wpa84126_single_project_archive' );

Make sure to change new_projects to the actual name you’ve registered the post type under. It will still behave in every way as if it’s the archive page, but only that single post will be queried. You could save the slug in an option so you could change it without having to edit the code.

Leave a Reply

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