Looks good.  You could eliminate the duplicated code by loading $page_ids with all the IDs first and then grabbing 100 of them at a time like:

    while ( $chunk = array_chunk( $page_ids, 100 ) ) {
        // Do stuff
    }

It requires N/100 more iterations, so up to you.  Both are faster than talking to the DB for each ID though ;)