Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Please check: How does accepting an answer work?. A heck of an Answer, by @s_ha_dum :) –  brasofilo Dec 4 '12 at 1:35

1 Answer 1

up vote 4 down vote accepted
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.

share|improve this answer
    
I wasn't aware of the page_attributes_dropdown_pages_args hook. I'll try it and post back. Thanks. –  41st Oct 17 '12 at 17:07
    
+1 Yes, that works - thanks! I will point out to other interested people that this snippet ONLY restricts the user's ability to nest pages below depth X. This does not prevent existing pages (which have already been designated as children of children [of children of etc...]) from appearing in the list of pages in the admin console, nor from being publicly accessible via its URL. (This may or may not be the behaviour what you are looking for.) –  41st Oct 17 '12 at 17:33
    
@41st +1 actually has a button for it! The actual one is mine ;) This one is a classic...! –  brasofilo Dec 4 '12 at 1:41
    
@brasofilo Indeed there is. At the time I did not have the requisite reputation to actually +1 anything. However, I've now returned from Hogwarts to give proper credit to the answer. –  41st Dec 5 '12 at 8:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.