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' );
post_parent = 0forpost_parent != 0and see the results. Maybe you coud create a second dropdown for this query. – brasofilo Apr 18 at 3:05post_parent = 0bit 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