I tried toUpdate:
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 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:
add_actionadd_filter( 'load-post.php''theme_page_templates', function( $page_templates, $obj, $post )
{
add_filter(// 'theme_page_templates',Restrict functionto the post.php loading
if( $page_templates,! $obj,did_action( $post'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' => 110,
'fields' => 'ids',
'meta_query' => [
[
'key' => '_wp_page_template',
'value' => $key,
'compare' => '=',
]
]
]
);
$count = count( $posts );
// Let's addAdd the "In use"/"Not in use" partscount to the theme'stemplate name:
in //the (thisdropdown. couldUse also10+ befor statistics>= instead)10
$page_templates[$key] = sprintf(
'%s (%s)',
$template,
0 == count($count $posts>= )10 ? __( 'Not in use!' )'10+' : __( 'In use' ) $count
);
}
return $page_templates;
}, 10, 3 );
} );
But because of how the get_page_templates()
method uses array_intersect_assoc()
on the cached values, after the theme_page_template
filter, this does not workExample: as excpected ;-)
One way around this would beHere we can see how it could look like, with the usage count info added to update the template dropdown via Javascript or just run this as a PHP function, within the backend.names :
Maybe I will check this again later or someone else
Hope you can take it from here, on howadjust this to update the template dropdown with statistics ;-)your needs!