The “Page Attributes” metabox is output by the page_attributes_meta_box() function (wp-admin/includes/meta-boxes.php, line 787), which uses the wp_dropdown_pages() function to generate the drop-down. The parameters passed to this function can be modified via the page_attributes_dropdown_pages_args filter. The argument you want to change is depth; by default this is 0, but changing it to 1 will show only top-level pages (Codex Reference).
add_filter( 'page_attributes_dropdown_pages_args', 'gowp_toplevel_parents_only', 10, 2 );
function gowp_toplevel_parents_only( $args, $post ) {
$args['depth'] = 1;
return $args;
}
Thanks.
But… I added that to my functions file, and there’s no change – the dropdown is still showing everything.
And editing the core file (wp-admin/includes/meta-boxes.php) also makes no difference.
Editing core files is never a good idea. However, if you edited the core file and nothing changed, either your host is heavily caching PHP or you’re editing the wrong files.
The code I provided works, I tested it on a local install.
Thanks.
I only edited that core file as a test.
I’ll look at it again later.
One other thing… can I add a conditional statement to that function so that it’s used on some pages but not others – for example, a specific CPT?
Absolutely. The function receives a WP_Post object via the second parameter ($post).
You can check for post type:
add_filter( 'page_attributes_dropdown_pages_args', 'gowp_toplevel_parents_only', 10, 2 );
function gowp_toplevel_parents_only( $args, $post ) {
if ( 'page' == $post->post_type ) {
$args['depth'] = 1;
}
return $args;
}
Or post ID:
add_filter( 'page_attributes_dropdown_pages_args', 'gowp_toplevel_parents_only', 10, 2 );
function gowp_toplevel_parents_only( $args, $post ) {
if ( in_array( $post->ID, array( 2, 3, 17 ) ) ) {
$args['depth'] = 1;
}
return $args;
}
Thanks.
Can I please ask one more thing… with CPT of ‘show’, what do I need to change in your code above? I can’t understand where to put the post_type.
Change 'page' to 'show' if that’s the name of your CPT.
Thanks.
I’ll have to investigate ‘your host is heavily caching PHP’, because I still can’t get this to work.