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