Bird
0
0

How can you modify the main query on the homepage to show only posts from the 'featured' category without creating a new WP_Query?

hard📝 Application Q9 of 15
Wordpress - Custom Theme Development
How can you modify the main query on the homepage to show only posts from the 'featured' category without creating a new WP_Query?
ACreate a new WP_Query with category_name 'featured'
BUse query_posts() inside the template file
CUse the pre_get_posts action hook to add category filter
DUse get_posts() with category filter
Step-by-Step Solution
Solution:
  1. Step 1: Understand modifying main query

    The recommended way to alter the main query is using the pre_get_posts action hook.
  2. Step 2: Explain why other options are less ideal

    query_posts() replaces the main query but is discouraged; creating new WP_Query or get_posts() does not modify the main query.
  3. Final Answer:

    Use the pre_get_posts action hook to add category filter -> Option C
  4. Quick Check:

    Modify main query with pre_get_posts [OK]
Quick Trick: Use pre_get_posts hook to change main query filters [OK]
Common Mistakes:
  • Using query_posts() which can cause issues
  • Creating new queries instead of modifying main query

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Wordpress Quizzes