That’s not how WP_Query
works, it isn’t what the documentation says either. WP_Query
is not a function.
foreach
needs an array, or something that can be iterated on, but you’ve given it a WP_Query
object.
Instead, look at the documentation or tutorials, all of them follow this basic pattern for a standard post loop:
$args = [
// parameters go here
];
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// display the post
the_title();
the_content();
}
wp_reset_postdata();
} else {
echo "no posts were found";
}