How to limit the pages displayed for choosing parent page on page attribute’s menu?

This meta box is printed with page_attributes_meta_box and the select field for choosing parent is generated with this code:

if ( is_post_type_hierarchical( $post->post_type ) ) :
    $dropdown_args = array(
        'post_type'        => $post->post_type,
        'exclude_tree'     => $post->ID,
        'selected'         => $post->post_parent,
        'name'             => 'parent_id',
        'show_option_none' => __('(no parent)'),
        'sort_column'      => 'menu_order, post_title',
        'echo'             => 0,
    );

    $dropdown_args = apply_filters( 'page_attributes_dropdown_pages_args', $dropdown_args, $post );
    $pages = wp_dropdown_pages( $dropdown_args );
    if ( ! empty($pages) ) :
?>
<p class="post-attributes-label-wrapper"><label class="post-attributes-label" for="parent_id"><?php _e( 'Parent' ); ?></label></p>
<?php echo $pages; ?>

As you can see the function wp_dropdown_pages is used in there and you can use page_attributes_dropdown_pages_args filter to modify args for its call.

So let’s say you want only top level pages in there:

function wpse_modify_parent_dropdown_args($args, $post) {
    $args['depth'] = 1;
    return $args;
}
add_filter( 'page_attributes_dropdown_pages_args', 'wpse_modify_parent_dropdown_args', 10, 2 );

Leave a Comment