I'd like to know it's possible to do something like this


I needn the post(45) only to have a custom template.

I know we can achieve this with page, like this


or by a templating file template-test.php

But I can't find if it's possible to do this with simple post (following the wordpress hierarchie, i think it's not possible)


  • what are you trying to achieve that requires that specific post to have a different template?
    – Tom J Nowell
    Feb 9, 2021 at 11:59

1 Answer 1


You have to follow this file name structure:


If you have a post names "Hello World" and the slug of post is hello-world then the template name will be single-post-hello-world.php


There is no default way to use id in template name. But you might try this snipped:

add_filter( 'single_template', 'load_post_template_by_id' );
function load_post_template_by_id( $single_template ) {
    global $post;
    if ( 'post' === $post->post_type) {
        $tmp_template = dirname( __FILE__ ) . '/single-post-'.$post->ID.'.php';

        if ( file_exists( $tmp_template ) ) {
            $single_template = $tmp_template;
    return $single_template;

Here I'm overriding the post template in a tricky way. Now if you have a post with id 42 and you have a template file named single-post-42.php then it should load automatically.

I didn't test the code. So use it with your own risk.

  • Is is possible now to do with the slug instead of the ID ? If the slug change, then the template will not work
    – lucrece
    Feb 9, 2021 at 14:14
  • I've updated my answer Feb 9, 2021 at 17:38
  • Thanks a lot ! Wish wordpress will add this feature in some update (even if it's very situational)
    – lucrece
    Feb 10, 2021 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.