WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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


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.