error with $query->have_posts()
Please use while ( have_posts() ) : the_post(); instead of foreach. See documentation for The Loop: https://developer.wordpress.org/themes/basics/the-loop/ If you want to foraech, You need to loop through $query->posts instead of $query.