7

When I create a new page within WordPress, I have the option of specifying which template to use from my theme (from a dropdown list on the right-hand side of the interface).

I need to find which of the available templates are unused, so that I can delete them.

How is this done please?

WP version is 4.2.2

2 Answers 2

6

What you need to do is compare the values of the meta field _wp_page_template, which contains the page template selected for a single page with the available page templates.

For this you need to construct an array of used templates, because you want the templates used by all the pages, similar as shown here:

Use array_unique to get unique values.

Then you need to get the available page templates, as shown here:

Last but not least, you can use array_diff to compare the arrays of used and available templates, which subsequently gives you the unused templates.

2
  • Great, thanks. As my strengths lie in database land rather than PHP, I used the following SQL code to provide a similar output, which can be tweaked later with counts, etc SELECT p.post_title as PostTitle, m.meta_value as Template FROM `wp_postmeta` as m join `wp_posts` as p ON p.ID = m.post_id where ((p.post_type = 'page') OR (p.post_type = 'post')) AND (m.meta_key = '_wp_page_template');. Your input was invaluable to pointing out where the correct database fields were.
    – EvilDr
    Commented May 18, 2015 at 11:53
  • 1
    My pleasure! Sure SQL is a way too. Just as a general tip, when available and suitable use WordPress API functions, it makes WP development easier in the long run. I said my piece :) @EvilDr Commented May 18, 2015 at 12:03
3

Update:

Page Template Usage Info in WordPress 4.4+

In WordPress 4.4 the array_intersect_assoc() was removed from the WP_Theme::get_page_templates() method.

See ticket #13265 and changeset #34995.

We can therefore add the page templates usage info, directly into the template dropdown, with the theme_page_templates filter, without using javascript or some clever object cache tricks explained here by @MikeSchinkel or here by @gmazzap.

Here's a demo (PHP 5.4+):

add_filter( 'theme_page_templates', function( $page_templates, $obj, $post )
{
    // Restrict to the post.php loading
    if( ! did_action( 'load-post.php' ) )
        return $page_templates;

    foreach( (array) $page_templates as $key => $template )
    {
        $posts = get_posts( 
            [
                'post_type'      => 'any',
                'post_status'    => 'any', 
                'posts_per_page' => 10,
                'fields'         => 'ids',
                'meta_query'     => [
                    [
                        'key'       => '_wp_page_template',
                        'value'     => $key,
                        'compare'   => '=',
                    ]
                ]
            ]
        );

        $count = count( $posts );

        // Add the count to the template name in the dropdown. Use 10+ for >= 10
        $page_templates[$key] = sprintf( 
            '%s (%s)', 
            $template, 
             $count >= 10 ? '10+' : $count
        );          
    }
    return $page_templates;
}, 10, 3 );

Example:

Here we can see how it could look like, with the usage count info added to the template names :

template usage info

Hope you can adjust this to your needs!

5
  • Neat idea, nearly impossible though to overcome the restriction by the core code - with PHP. The page_template_dropdown() function also doesn't give the possibility. Commented May 18, 2015 at 12:28
  • Sometimes it's just one line of code in the core that makes the difference ;-) Javascript could be one way to adjust the template dropdown, or one could try preg_replace() on the output from page_attributes_meta_box() to swap the dropdown, or just try to extend the WP_Theme class. Or some other way ;-) @ialocin
    – birgire
    Commented May 18, 2015 at 12:42
  • Sure sometimes it is just one line and sometimes it is almost all of it :) And sure, where there is a will there's a way. Commented May 18, 2015 at 12:45
  • 1
    hehe and sometimes it's just WordPress ;-)
    – birgire
    Commented May 18, 2015 at 12:46
  • Looks like WordPress 4.4 was the lucky version ;-) @ialocin
    – birgire
    Commented Nov 19, 2016 at 13:46

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.