WP_Query() not filtering posts for category

the_post() operates on the main query, but this isn’t the main query you’re looping over, you need to use $q->the_post()

Remember:

  • the_post() is the same as global $wp_query; $wp_query->the_post();
  • All post loops are WP_Query post loops if you dig down deep enough, even get_posts has a WP_Query inside it
  • There can only be 1 current post in $post, calling the_post sets that global variable.

This is what a standard post loop should look like:

$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 {
    esc_html_e( 'no posts were found', 'textdomain' );
}