Wordpress - View WordPress page template usage (or unused)

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:

  • Return all custom meta data for one custom post type

Use array_unique to get unique values.

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

  • get page templates

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.


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!