I am creating a plugin that registers a custom post type, and I want it to be displayed using the theme's page.php template, rather than the post.php. I set the hierarchical
flag when I register it, but the post.php template is still called.
Way, way down inside wp-includes/post.php, where it appears to decide which template to use, is the code:
if ( ! empty( $postarr['page_template'] ) && 'page' == $data['post_type'] ) {
$post->page_template = $postarr['page_template'];
$page_templates = wp_get_theme()->get_page_templates( $post );
...
which makes me think that the post template is used unless post_type
is exactly 'page'
.
I don't want to define a template of my own, because I want to work with any theme. I just want to tell WordPress which of the current theme's templates to use.
There's an action hook, called get_template_part_{$slug}
that might do the trick, but I'm not sure what it expects, and the documentation for that one is unusually spotty.