Bird
0
0

Which of the following is the correct way to query posts from category with ID 5 using WP_Query?

easy📝 Syntax Q12 of 15
Wordpress - WordPress Query and Database
Which of the following is the correct way to query posts from category with ID 5 using WP_Query?
A$query = new WP_Query(array('category' => 5));
B$query = new WP_Query(array('cat' => 5));
C$query = new WP_Query(array('category_name' => 5));
D$query = new WP_Query(array('category_id' => 5));
Step-by-Step Solution
Solution:
  1. Step 1: Recall correct parameter for category ID

    The correct parameter to filter by category ID is 'cat', not 'category', 'category_name', or 'category_id'.
  2. Step 2: Verify syntax correctness

    $query = new WP_Query(array('cat' => 5)); uses 'cat' with the ID 5 correctly in an array for WP_Query.
  3. Final Answer:

    $query = new WP_Query(array('cat' => 5)); -> Option B
  4. Quick Check:

    Category ID filter uses 'cat' = C [OK]
Quick Trick: Use 'cat' key for category ID in WP_Query parameters [OK]
Common Mistakes:
  • Using 'category' instead of 'cat' for category ID
  • Passing category ID as 'category_name'
  • Using 'category_id' which is invalid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Wordpress Quizzes