4

I want to be able to have a link like this, site.com/my-custom-type-slug/single-custom-post-name/stats/, where /stats should contain the /my-custom-type-slug page content and when I add content to the /stats page, it should be added after the parent's content.

The /stats page should be available for every page created within my CPT, for example /my-custom-type-slug2, /my-custom-type-slug3, and so on, to have the /stats.

I'm thinking that a solution can be to create a page named stats and assign a custom template, but the problem is how can I create the additional content for every new post which I'm creating?

This would be easy if I didn't need the comments to be activated because I was doing it like this:

Add a custom WYSIWYG field inside my posts with ACF plugin, then create an endpoint:

function wpa121567_rewrite_endpoints(){
    add_rewrite_endpoint( 'stats', EP_PERMALINK );
}
add_action( 'init', 'wpa121567_rewrite_endpoints' );

After this my URL would look like this:
site.com/your-custom-type-slug/single-custom-post-name/stats/

Then in my single-{cpt}.php template I can check if the request is for stats, and include or output the desired data:

if( array_key_exists( 'stats', $wp_query->query_vars ) ){
    // the request is for the comments page
} 
else {
    // the request is for the main post
}

This solution is not working for me because I can't activate comments on /stats because I'm using an endpoint to create it.

As a conclusion, what I need is two separate sets of comments for both the parent and "/stats" page. Any suggestions on how to achieve this?

1

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; ?>

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.