[wp-trac] [WordPress Trac] #41773: Page Templates // Post Type Templates | Any Post Type?
WordPress Trac
noreply at wordpress.org
Thu May 8 06:12:53 UTC 2025
#41773: Page Templates // Post Type Templates | Any Post Type?
-------------------------------------+-------------------------------------
Reporter: michael.ecklund | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Posts, Post Types | Version: 4.7
Severity: normal | Resolution:
Keywords: reporter-feedback has- | Focuses: ui, administration,
patch | template
-------------------------------------+-------------------------------------
Comment (by sahilgidwani):
Hi @michaelecklund ,
I've submitted a PR that proposes a core-level fix for this issue by
enhancing the logic in WP_Theme::get_post_templates() to support a
Template Post Type: any header. This allows a single template to be made
available across all public post types without having to explicitly list
them.
In the meantime, if you’d like to achieve similar functionality without
modifying core, you can use the following workaround by hooking into the
init action and applying the theme_{$post_type}_templates filter
dynamically:
{{{#!php
add_action('init', function () {
$post_types = get_post_types(['public' => true], 'names');
foreach ($post_types as $post_type) {
add_filter("theme_{$post_type}_templates", function ($templates) {
$templates['your-template-file.php'] = 'Your Template Label';
return $templates;
});
}
});
}}}
This will register your custom template across all public post types
regardless of what’s declared in the template header.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/41773#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list