Limit number of pages that use a specific template?

You can approach this by first using a database query to count the number of pages that are already using the template:

$query = "SELECT COUNT(*) as total
    FROM prefix_posts as p JOIN prefix_postmeta as m ON p.ID = m.post_id
    WHERE p.'post_type' = 'page'
    AND p.'post_status' = 'publish'
    AND m.'meta_key'    = '_wp_page_template'
    AND m.'meta_value'  = 'page-your-template-name.php'";

Then check if your count limit has been reached and remove the page template with the theme_page_templates filter:

Leave a Comment