I found a snippet to add a dropdown to filter pages by parent in the admin. I modified it to work for a hierarchical custom post type. It works, but it is from an old post, so am wondering if the code needs updating?

Also, is there a way to show the parent and all children and grandchildren? This just shows the top level children, but not the parent or any lower level children.

Here is the code:

function cbt_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', 'cbt_admin_posts_filter' );

function admin_page_filter_parentpages() { 
 global $wpdb;   
if (isset($_GET['post_type']) && $_GET['post_type'] == 'sites') {
    $sql = "SELECT ID, post_title FROM ".$wpdb->posts." WHERE post_type = 'sites' AND post_parent = 0 AND post_status = 'publish' ORDER BY post_title";
 $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 .= '
    echo $select;
} else {
add_action( 'restrict_manage_posts', 'admin_page_filter_parentpages' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.