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.

I have the following page hierarchy, all of which are under a top page (TOP) with ID = 1:

  • mid1
    • gc1
    • gc2
  • mid2
    • gc3
    • gc4

I would like to make submit WP_Query query that will return random pages that are grandchildren of TOP - in this example, I want to randomly select from gc1, gc2, gc3 and gc4, but exclude mid1 and mid2. What is the best way to do this?

share|improve this question

1 Answer 1

My current idea is:

  1. Use get_pages to get all of the descendants of TOP and put the IDs in an array arrayAll.
  2. Then run get_pages again with parent=1 to get just the direct children of TOP. Put these in array arrayChildren.
  3. Run in php arrayGrandchildren = array_diff(arrayAll, arrayChildren) to get a list of all pages from the first array (which contains all descendants of TOP) that do not include the direct children.
  4. In WP_Query, set 'post_in=arrayGrandchildren&order=rand' to get random posts from the list of grandchildren.
share|improve this answer

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.