0
0
Wordpressframework~10 mins

WP_Query class in Wordpress - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new WP_Query object.

Wordpress
$query = new [1](['post_type' => 'post']);
Drag options to blanks, or click blank then click option'
APost_Query
BQuery_WP
CQueryPost
DWP_Query
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect class names like 'Query_WP' or 'Post_Query'.
Forgetting to use 'new' keyword.
2fill in blank
medium

Complete the code to check if the query has posts.

Wordpress
if ($query->[1]()) { echo 'Posts found'; }
Drag options to blanks, or click blank then click option'
Ahave_posts
Bposts_exist
Chas_posts
Dfound_posts
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'has_posts()' instead of 'have_posts()'.
Using properties like 'found_posts' instead of methods.
3fill in blank
hard

Fix the error in the loop to display post titles.

Wordpress
while ($query->[1]()) { $query->the_post(); the_title(); }
Drag options to blanks, or click blank then click option'
Ahave_posts
Bnext_post
Cget_post
Dthe_post
Attempts:
3 left
💡 Hint
Common Mistakes
Using the_post() in the while condition (it does not return a boolean).
Using non-existent methods like get_post() or next_post().
4fill in blank
hard

Fill both blanks to create a query for 5 recent posts of type 'product'.

Wordpress
$args = ['post_type' => [1], 'posts_per_page' => [2]];
$query = new WP_Query($args);
Drag options to blanks, or click blank then click option'
A'product'
B5
C'post'
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'post' instead of 'product' for post_type.
Setting posts_per_page to 10 instead of 5.
5fill in blank
hard

Fill all three blanks to loop through posts and display their titles with links.

Wordpress
if ($query->[1]()) {
  while ($query->[2]()) {
    $query->the_post();
    echo '<a href="' . get_permalink() . '">' . [3]() . '</a><br>';
  }
}
Drag options to blanks, or click blank then click option'
Ahave_posts
Bthe_post
Cthe_title
Dget_posts
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get_posts()' which returns an array, not a method of WP_Query.
Mixing up 'the_post()' and 'have_posts()' in the loop.