Hello I feel like my WP theme folder is getting a bit messy is there a way to put all the custom page-{post_type}.php, and single-{post_type}.php inside a folder instead of having them in the root of the theme?.

And how to make sure WP reads them? Thanks in advance

  • Similar discussion: wordpress.stackexchange.com/questions/227006/…. Oh, and by the way, Wordpress themes are messy, because there's no clear separation of presentation and logic. Just look at the default themes. If your requirements get more and more complex, you might want to look into alternatives like Laravel to build your frontend.
    – Hans
    Sep 22, 2018 at 16:39
  • @Michael But let's face it. For any competitive theme out there, and no, I'm not talking about these "lie to my face" themes such as OceanWP, that require you to get about 50 plugins to actually get to build your site, it's impossible to separate the view from the logic. The biggest thing that helps with it, but not always is using actions - but then again, there are frequent times when, for clarity's sake, you might not want to send the developers looking at your code through a never ending hole, and of course, if that logic is absolute to your theme's functionality, then yea - put it in.
    – coolpasta
    Sep 22, 2018 at 17:07
  • @Michael I fully agree with coolpasta, even in Drupal with twig templates, you can never fully separate view and model. You'll always have variables in your view.
    – Pim
    Sep 24, 2018 at 13:43
  • @Coolpaste I think you're right about WP themes, it will be hard to seperate the two in most cases. WP is just not built that way. Pim, a variable is not equal to logic, is it? The whole premise of frameworks like Laravel or Symfony is to seperate concerns, which might not be necessary until you reach a certain complexity or scale of your application. At least that is my experience.
    – Hans
    Sep 24, 2018 at 15:03

1 Answer 1


i was able to solve some of it

    // defining the sub-directory so that it can be easily accessed from elsewhere as well.
define( 'WPSE_PAGE_TEMPLATE_SUB_DIR', 'pages' );

function wpse227006_page_template_add_subdir( $templates = array() ) {
    // Generally this doesn't happen, unless another plugin / theme does modifications
    // of their own. In that case, it's better not to mess with it again with our code.
    if( empty( $templates ) || ! is_array( $templates ) || count( $templates ) < 3 )
    return $templates;

$page_tpl_idx = 0;
$cnt = count( $templates );
if( $templates[0] === get_page_template_slug() ) {
    // if there is custom template, then our page-{slug}.php template is at the next index 
    $page_tpl_idx = 1;

// the last one in $templates is page.php, so
// all but the last one in $templates starting from $page_tpl_idx will be moved to sub-directory
for( $i = $page_tpl_idx; $i < $cnt - 1; $i++ ) {
    $templates[$i] = WPSE_PAGE_TEMPLATE_SUB_DIR . '/' . $templates[$i];

return $templates;
// the original filter hook is {$type}_template_hierarchy,
// wihch is located in wp-includes/template.php file
add_filter( 'page_template_hierarchy', 'wpse227006_page_template_add_subdir' );

But im still looking for the same solution for my single-{post-type}.php pages to be put inside a folder. and for my taxonomy-{taxonomy-name}.php :) if anyone would be so kind to guide me in the right direction.

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.