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?

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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