For this situation, I create a custom page template, say tpl-archive-page.php
.
For example using Advanced Custom Fields and the Post Type Selector Field(*) the user can choose a post type to connect to.
<?php
/**
* Template Name: CPT Archive Page
*/
get_header();
while (have_posts()) :
the_post();
get_template_part('content', 'page');
$archive_query_post_type = get_post_meta(get_queried_object_id(), 'cpt_archive_query_post_type', true);
if( $archive_query_post_type && is_string($archive_query_post_type) ) {
$args = array(
'post_type' => $archive_query_post_type,
'posts_per_page'=> -1,
'orderby' => 'title',
);
$archive_query = new WP_Query( $args );
if ( $archive_query->have_posts() ) {
while ( $archive_query->have_posts() ) {
$archive_query->the_post();
get_template_part('content', get_post_type() );
}
}
wp_reset_postdata();
}
endwhile;
get_footer();
The has_archive
option when registering the custom post type must be set to false
.
(*) If you are using ACF5/Pro, consider this fork of the field.