Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
Is the goal to have the particular posts show only specific pages on front end or back end of the site? If it's the front end, have you tried a using Conditional Tags: <?php if ( is_page_template('single.php')) { /* Your Code Here */ } –  Jeremy Jared Sep 13 '12 at 3:00
    
I'd like to show them on the backend, in the Dashboard. Basically, I want to copy the Pages listing page but only show pages that have, say, the homepage.php template applied to them. –  lancemonotone Sep 13 '12 at 14:06

2 Answers 2

up vote 1 down vote accepted

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;
}
share|improve this answer
1  
Thanks jessica. I answered my own question before I saw your reply but yours looks like it would work too, and is more elegant. Nice. –  lancemonotone Sep 14 '12 at 18:28
    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;
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.