add wpdb as global on the second select, that will be works, always