Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using the code below (adapted from this source) to add a parent page filter to the list of pages in wp-admin.

It seems to work a treat, but only at a depth of 1. I would like to return results at all depths, i.e. children and grandchildren etc.

Any tips, or alternative suggestions for creating this filter?

Thanks!

function fws_admin_posts_filter( $query ) {
    global $pagenow;
    if ( is_admin() && $pagenow == 'edit.php' && !empty($_GET['my_parent_pages'])) {
        $query->query_vars['post_parent'] = $_GET['my_parent_pages'];
    }
}
add_filter( 'parse_query', 'fws_admin_posts_filter' );
function admin_page_filter_parentpages() {
    global $wpdb;
    if (isset($_GET['post_type']) && $_GET['post_type'] == 'page') {
        $sql = "SELECT ID, post_title FROM ".$wpdb->posts." WHERE post_type = 'page' AND post_parent = 0 AND post_status = 'publish' ORDER BY menu_order";
        $parent_pages = $wpdb->get_results($sql, OBJECT_K);
        $select = '
            <select name="my_parent_pages">
                <option value="">Parent Pages</option>';
        $current = isset($_GET['my_parent_pages']) ? $_GET['my_parent_pages'] : '';
        foreach ($parent_pages as $page) {
            $select .= sprintf('
                <option value="%s"%s>%s</option>', $page->ID, $page->ID == $current ? ' selected="selected"' : '', $page->post_title);
        }
        $select .= '
            </select>';
        echo $select;
    } else {
        return;
    }
}
add_action( 'restrict_manage_posts', 'admin_page_filter_parentpages' );
share|improve this question
I really don't understand much what's the goal here... In the SQL query, change post_parent = 0 for post_parent != 0 and see the results. Maybe you coud create a second dropdown for this query. – brasofilo Apr 18 at 3:05
Hi brasofilo. The post_parent = 0 bit gives me a dropdown with all top level pages - perfect. What I want is the results that are displayed to be not just children of these top level pages but also grandchildren. Does that makes sense? I suppose this is the bit I have to change?: $query->query_vars['post_parent'] = $_GET['my_parent_pages']; – Caroline Elisa Apr 29 at 20:44
No, I don't get it. You're repeating almost the same idea as in the Question content. Do you want a full tree, just like already exists in the dashboard? What are those child and grandchild? A visual representation would help. Please, add new info to the Q itself (edit). – brasofilo Apr 29 at 20:55

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

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.