View WordPress page template usage (or unused)

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 s
2

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.

Leave a Comment