A. 'category' should be 'category_name' to filter by category slug
B. 'posts_per_page' must be a string, not a number
C. 'category' should be 'cat' with category slug
D. No error, this code works correctly
Solution
Step 1: Check parameter for category slug
To filter by category slug (like 'news'), the correct parameter is 'category_name', not 'category'.
Step 2: Validate other parameters
'posts_per_page' accepts a number, so that part is correct.
Final Answer:
'category' should be 'category_name' to filter by category slug -> Option A
Quick Check:
Category slug filter uses 'category_name' = A [OK]
Hint: Use 'category_name' for slug, 'cat' for ID in queries [OK]
Common Mistakes:
Using 'category' instead of 'category_name' for slug
Thinking 'posts_per_page' must be string
Confusing 'cat' with slug instead of ID
5. You want to display posts that belong to category ID 4 or category ID 7 using WP_Query. Which query parameter array will correctly achieve this?
hard
A. array('category_name' => '4,7')
B. array('cat' => '4,7')
C. array('category__in' => array(4, 7))
D. array('cat' => array(4, 7))
Solution
Step 1: Understand how to query multiple categories
To query posts in multiple categories by ID, use 'category__in' with an array of IDs.
Step 2: Evaluate each option
array('cat' => '4,7') uses a string with comma which is invalid for 'cat'. array('category_name' => '4,7') uses 'category_name' with numbers which expects slugs. array('cat' => array(4, 7)) uses 'cat' with an array which is invalid.
Final Answer:
array('category__in' => array(4, 7)) -> Option C
Quick Check:
Multiple category IDs use 'category__in' with array = A [OK]
Hint: Use 'category__in' with array for multiple category IDs [OK]
Common Mistakes:
Passing multiple IDs as comma string to 'cat'
Using 'category_name' with numeric IDs
Passing array to 'cat' which expects single ID
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.