Filter Pages by Template
Adds a template dropdown above the Pages list so you can show only the pages that use a given page template, such as every landing page or full-width page.
Opens WPIDE on
Code
add_action( 'restrict_manage_posts', function ( $post_type ) {
if ( 'page' !== $post_type ) {
return;
}
$current = isset( $_GET['wpide_template'] ) ? sanitize_text_field( wp_unslash( $_GET['wpide_template'] ) ) : '';
$templates = wp_get_theme()->get_page_templates( null, 'page' );
echo '<label class="screen-reader-text" for="wpide-template-filter">Filter by template</label>';
echo '<select name="wpide_template" id="wpide-template-filter"><option value="">All templates</option>';
printf( '<option value="default"%s>Default template</option>', selected( $current, 'default', false ) );
foreach ( $templates as $file => $name ) {
printf( '<option value="%s"%s>%s</option>', esc_attr( $file ), selected( $current, $file, false ), esc_html( $name ) );
}
echo '</select>';
} );
add_action( 'pre_get_posts', function ( $query ) {
if ( ! is_admin() || ! $query->is_main_query() || empty( $_GET['wpide_template'] ) || 'page' !== $query->get( 'post_type' ) ) {
return;
}
$template = sanitize_text_field( wp_unslash( $_GET['wpide_template'] ) );
if ( 'default' === $template ) {
$query->set( 'meta_query', array(
'relation' => 'OR',
array( 'key' => '_wp_page_template', 'compare' => 'NOT EXISTS' ),
array( 'key' => '_wp_page_template', 'value' => array( '', 'default' ), 'compare' => 'IN' ),
) );
return;
}
$query->set( 'meta_query', array( array( 'key' => '_wp_page_template', 'value' => $template ) ) );
} );
Notes
"Default template" shows pages that do not use a custom template.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.