What is the optimal way to filter out subpages from admin?

I guess it’s always possible to hide them with js, but is there a code snippet out there for query level child page filter? I only want top level pages to remain.

EDIT: my coder came up with it, stay tuned for a solution

Related posts

Leave a Reply

1 comment

  1. This works for our needs:

    # Parent filtering
    function wps_restrict_manage_posts() {
        global $typenow;
    
        # only run this filter on page post type
        if ( $typenow == 'konverentsid' ) {
            # Parent Page Filter (HTML)
            $args = array(
                'post_type' => 'konverentsid',
                'child_of' => 0,
                'show_option_none' => __('Show all pages'),
                'depth' => 1,
                'name' => 'wps_page_filter',
                'selected' => $_GET['wps_page_filter'],
            );
            $dropdown = wp_dropdown_pages( $args );
        }
    
    }
    add_action ( 'restrict_manage_posts' , 'wps_restrict_manage_posts' );
    
    # Add an option to show top level pages only
    add_filter ( 'wp_dropdown_pages' , 'wps_insert_option_in_dropdown' );
    function wps_insert_option_in_dropdown ( $output ) {
        $filter = $_GET['wps_page_filter'];
        if ( $filter == 'parent' )
            $selected = ' selected="selected"';
        else
            $selected = '';
        $second_option = __("Show all pages") . '</option><option value="parent"' . $selected . '>' . __("Parent Pages Only") . '</option>';
        return preg_replace( '#Show all pages</option>#' , $second_option , $output );
    }
    add_filter ( 'parse_query', 'wps_admin_posts_filter' );
    
    function wps_admin_posts_filter( $query ) {
        global $pagenow;
    
        # Change query based on post_parent
        $wps_page_filter = $_GET['wps_page_filter'];
        if ( is_admin() && $pagenow == 'edit.php' && !empty( $wps_page_filter ) ) {
            if ( $wps_page_filter == 'parent' )
                $query->query_vars['post_parent'] = 0;
            else
                $query->query_vars['post_parent'] = $wps_page_filter;
        }
    }