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.

Wordpress has stumped me for the first time in a very long time. I am trying to limit the depth of a get_posts call in my template. I want to get children but limit it to a depth of only 3 as I am generating a faux menu.

Is there a way perhaps through a subquery I can limit the depth without excluding pages or checking the page_parent value 3 times?

share|improve this question
    
You can't limit the depth directly via get_posts/WP_Query, a post only contains the value of its parent, so you'd have to check the parent, but you said 3, not 2 or more, so you'd have to check the whole way up the tree, and WP_Query isn't equipped for that ( and it shouldn't be ) –  Tom J Nowell Jun 11 at 0:15
    
One way around it would be to maintain some post meta labeling for the root_parent-children-grandchildren relationship. Another idea would be to maintain a record of all the ID's in that circle and feed it to the query. –  birgire Jun 11 at 0:47
    
... or automatically maintain the depth as a post meta value for each page. Then you can use a meta query to retrieve all pages with depth < 3. But the bulk actions or branching might be a challenge ;-) –  birgire Jun 11 at 1:07

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.