Create child page within custom post type

I suggest a hierarchical custom post type and a conditional to create an additional loop on your single-{cpt}.php. By using hierarchical custom post type, you can create a sub-cpt , like a sub page as the stats part of it’s parent post.

The sub-cpt then can be used to store additional data (e.g. in post_content or custom_fields) and also the comment specific to the stats part of the post.

Note that you will need to include only parent cpt (excluding the sub cpt) on the main loop using the pre_get_posts hook.

On the single-{cpt}.php it should be something like this

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php if( array_key_exists( 'stats', $wp_query->query_vars ): ?>

        <!-- output the data for the stats part -->
        <?php
            // query sub cpt data
            $args = array(
                'post_parent' => $post->ID,
                'post_type'   => 'your-cpt', 
                'posts_per_page' => 1,
                'post_status' => 'any'
            );

            $sub_cpt = get_children( $args);

            // query sub cpt comment
            $args = array(
                'post_id' => $sub_cpt->ID,
            );

            $child_post_comment = get_comments( $comment_args );
        ?>

    <?php else: ?>

        <!-- output the data as you intended for the non stats part -->
        <?php the_title(); ?>
        <?php the_content(); ?>
        <?php
            // query sub cpt comment
            $args = array(
                'post_id' => $post->ID,
            );
        ?>
        <?php get_comments( $args ); ?>

    <?php endif; ?> 
<?php endwhile; ?>
<?php endif; ?>

Leave a Comment