# 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.

- Type: PHP
- Runs: Admin only
- Category: [Admin](https://wpide.com/library/category/admin/)
- Tags: admin-filters, pages, templates
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/admin-filter-pages-by-template/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Filter Pages by Template" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
