Customize edit.php Pages listing in dashboard to show only pages with a particular template applied?

The question says it all, I think. I’d like to create a new submenu page using edit.php which will only display pages which use a certain template file (or files).

Possible?

Related posts

Leave a Reply

2 comments

  1. Try this:

    <?php
    
    // add submenu page
    add_action('admin_menu', 'add_template_submenu');   
    function add_template_submenu()
    {
        add_pages_page( 'My Template', 'My Template', 'edit_pages', 'edit.php?post_type=page&template=your-template-file.php');
    }
    
    // check for the template name passed in $_GET and add it to the query
    add_filter('parse_query', 'filter_pages');  
    function filter_pages($query)
    {
        global $typenow, $pagenow;
        if ($pagenow == 'edit.php' && $typenow == 'page' && $_GET['template'])
        {
            $query->query_vars['meta_key'] = '_wp_page_template';
            $query->query_vars['meta_value'] = $_GET['template'];
        }
        return $query;
    }
    
  2.     add_action('admin_menu','add_home_page_menu');
        add_action('load-edit.php', 'filter_home_pages_mysql_action'); 
    
        function add_home_page_menu(){
            add_pages_page('Home Pages Menu', 'Home Pages', 'manage_options', 'edit.php?post_type=page&page_template=home_pages');
            add_pages_page('Landing Pages Menu', 'Landing Pages', 'manage_options', 'edit.php?post_type=page&page_template=landing_pages');
        }
    
    
        function filter_home_pages_mysql_action() {
            if ($_GET['post_type'] !== 'page') return;
            switch($_GET['page_template']){
                case 'home_pages':
                    add_filter('posts_where', 'home_pages_where_filter') );
                    add_filter('posts_join', 'join_filter') );
                    break;
                case 'landing_pages':
                    add_filter('posts_where', 'landing_pages_where_filter') );
                    add_filter('posts_join', 'join_filter') );
                    break;
                default:
                    break;
            }
        }
    
        function home_pages_where_filter($sql) {
            global $wpdb;
            $sql = " AND $wpdb->postmeta.meta_key = '_wp_page_template' AND $wpdb->postmeta.meta_value LIKE 'template-homepage-grid-%.php'" . $sql;
            return $sql;
        }
    
        function landing_pages_where_filter($sql) {
            global $wpdb;
            $sql = " AND $wpdb->postmeta.meta_key = '_wp_page_template' AND $wpdb->postmeta.meta_value LIKE 'template-landing.php'" . $sql;
            return $sql;
        }
    
        function join_filter($sql) {
            global $wpdb;
            $sql = " JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id " . $sql;
            return $sql;
        }