0
0
Wordpressframework~30 mins

Query parameters in Wordpress - Mini Project: Build & Apply

Choose your learning style9 modes available
WordPress Query Parameters
📖 Scenario: You are building a WordPress site that shows a list of blog posts. You want to control which posts appear by using query parameters in your code.
🎯 Goal: Create a WordPress query using query parameters to display posts from a specific category and limit the number of posts shown.
📋 What You'll Learn
Create an array called $args with specific query parameters
Add a variable $posts_per_page to control how many posts to show
Use WP_Query with the $args array to get posts
Complete the loop to display the post titles
💡 Why This Matters
🌍 Real World
WordPress developers often need to customize which posts appear on a page by using query parameters to filter posts by category, status, or other criteria.
💼 Career
Understanding WP_Query and query parameters is essential for WordPress theme and plugin developers to create dynamic and customized content displays.
Progress0 / 4 steps
1
Create the query parameters array
Create an array called $args with these exact entries: 'category_name' => 'news' and 'post_status' => 'publish'.
Wordpress
Need a hint?

Use array() to create $args with keys and values exactly as shown.

2
Add a posts per page variable
Add a variable called $posts_per_page and set it to 5. Then add this to the $args array with the key 'posts_per_page'.
Wordpress
Need a hint?

Define $posts_per_page as 5 and add it to $args with the key 'posts_per_page'.

3
Create the WP_Query object
Create a variable called $query and set it to a new WP_Query object using the $args array.
Wordpress
Need a hint?

Use new WP_Query($args) to create the query object.

4
Complete the loop to display post titles
Write a while loop that runs while $query->have_posts() is true. Inside the loop, call $query->the_post() and then output the post title using the_title().
Wordpress
Need a hint?

Use a while loop with $query->have_posts(), call $query->the_post() inside, then output the title with the_title().