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.

I've set up a menu in the backend that shows a list of pages that have a specific page template assigned to them. I've managed to do this using the code below, but I also want to display the child pages for each of these pages. The child pages do not use the same page template. Any idea how I can do this?

add_action('admin_menu','add_hub_page_menu');
add_action('load-edit.php', 'filter_hub_pages_mysql_action'); 

function add_hub_page_menu(){
    add_pages_page('Hub Pages', 'Hub Pages', 'manage_options', 'edit.php?post_type=page&page_template=hub_template');
}

function filter_hub_pages_mysql_action() {
    if ($_GET['post_type'] !== 'page') return;
    switch($_GET['page_template']){
        case 'hub_template':
            add_filter('posts_where', 'hub_pages_where_filter' );
            add_filter('posts_join', 'join_filter' );
            break;
        default:
            break;
    }
}

function hub_pages_where_filter($sql) {
    global $wpdb;
    $sql = " AND $wpdb->postmeta.meta_key = '_wp_page_template' AND $wpdb->postmeta.meta_value LIKE 'hub-template.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 question

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.