Modify query to exclude certain ids and certain parents and its corresponding children

Turns out that it is possible to run a WP_Query inside a pre_get_posts action keeping in mind two simple principles:

  1. pre_get_posts action will alter every query that you make, so you need to contain its action specifying which queries you need to alter. In this case, this containment is done with this line:

    if (
      is_admin()
      and !current_user_can('manage_options')
      and $screen->post_type == 'page'
    ) {
      ...
    }
    

    This means that I’m altering only the query that happens when you load edit.php?post_type=page in Administration Panel.

  2. Because of the former, pre_get_posts will alter the main query of the page, which is “query all pages“. My question attempted to run a WP_Query inside an action that alters every query, so this action was also altering my “sub-queries”. So, as the first principle tells us, we need to contain the action to act only on the main query, because if we don’t contain it, we’ll get an infinite loop. So, our “containment” line needs to know if what we’re altering is actually the main query:

    if (
      is_admin()
      and !current_user_can('manage_options')
      and $screen->post_type == 'page'
      and $query->is_main_query()  // This contains only the main query
    ) {
      ...
    }
    

    So now we can run whatever WP_Query we want in the get_dummy_pages() function.