Is there a way to limit the creation of pages (custom post type) to a specific depth – e.g. level 1 (where 0 = parent, 1 = child, 2 = grand-child, etc.)?

For example, let’s create a ‘Summer’ recipe page (custom post type: Recipe) with a slug ‘/summer’. Let’s now create a ‘Pie’ page (a child of ‘Summer’) with a slug ‘/summer/pie’. I want to prevent the user from creating a page which is a child of ‘Pie’ or of any other sub-page.

  • example.com/summer -> GOOD
  • example.com/summer/pie -> GOOD
  • example.com/summer/pie/apple -> BAD
  • example.com/summer/pie/blackberry -> BAD

Thanks.

2 s
2

function my_test($a) {
  $a['depth'] = 1;
  return $a;
}
add_action('page_attributes_dropdown_pages_args','my_test');

Put that in a theme’s function.php or in a plugin.

Leave a Reply

Your email address will not be published. Required fields are marked *