<?php $query = new WP_Query(['category_name' => 'news', 'posts_per_page' => 3]); if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); echo get_the_title() . '\n'; } } wp_reset_postdata(); ?>
The WP_Query is set to fetch posts from the 'news' category only, limited to 3 posts. The loop outputs their titles.
Option D is a valid PHP associative array with correct syntax. Option D misses a comma. Option D misses quotes around the key. Option D uses a string 'five' instead of an integer for posts_per_page, which is allowed but not recommended; however, it won't cause syntax error but may cause unexpected behavior.
<?php $query = new WP_Query(['tag' => 'featured']); $count = 0; if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); $count++; } } wp_reset_postdata(); ?>
The variable $count increases by 1 for each post found with the 'featured' tag. So it equals the number of such posts.
<?php $query = new WP_Query(['category' => 'news']); if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); echo get_the_title() . '\n'; } } wp_reset_postdata(); ?>
The parameter 'category' is not recognized by WP_Query. To filter by category slug, use 'category_name'. To filter by category ID, use 'cat'.
To get posts in either category 4 or 7, use 'category__in' with an array of IDs. 'cat' expects a comma-separated string but treats it as posts in both categories (AND). 'category__and' fetches posts in both categories simultaneously. 'category_name' expects slugs, not IDs.