0
0
Wordpressframework~20 mins

Pagination with custom queries in Wordpress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pagination Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this WordPress custom query pagination code?

Consider this WordPress loop with a custom query and pagination. What will be the output on the second page?

Wordpress
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = [
  'post_type' => 'post',
  'posts_per_page' => 3,
  'paged' => $paged
];
$query = new WP_Query($args);
if ($query->have_posts()) :
  while ($query->have_posts()) : $query->the_post();
    the_title('<h2>', '</h2>');
  endwhile;
  echo paginate_links(['total' => $query->max_num_pages, 'current' => $paged]);
endif;
wp_reset_postdata();
?>
AThrows a fatal error because 'paginate_links' requires a query argument
BDisplays posts 1 to 3 again because 'paged' is ignored, with pagination links
CShows no posts because 'paged' is not set correctly, but pagination links appear
DDisplays posts 4 to 6 with their titles and pagination links for total pages
Attempts:
2 left
💡 Hint

Check how the 'paged' parameter controls which posts appear on each page.

📝 Syntax
intermediate
1:30remaining
Which option correctly sets up pagination for a custom WP_Query?

Choose the correct way to pass the current page number to a custom WP_Query for pagination.

A
$paged = get_query_var('page') ?: 1;
$args = ['paged' =&gt; $paged];
B
$paged = $_GET['page'] ?? 1;
$args = ['paged' =&gt; $paged];
C
$paged = get_query_var('paged') ?: 1;
$args = ['paged' =&gt; $paged];
D
$paged = get_query_var('paged') ?: 0;
$args = ['paged' =&gt; $paged];
Attempts:
2 left
💡 Hint

WordPress uses 'paged' query var for pagination, not 'page'.

🔧 Debug
advanced
2:00remaining
Why does this custom query pagination always show the first page posts?

Look at this code snippet. Why does pagination not work and always show the first page posts?

Wordpress
<?php
$args = [
  'post_type' => 'post',
  'posts_per_page' => 5
];
$query = new WP_Query($args);
if ($query->have_posts()) :
  while ($query->have_posts()) : $query->the_post();
    the_title('<h3>', '</h3>');
  endwhile;
  echo paginate_links(['total' => $query->max_num_pages]);
endif;
wp_reset_postdata();
?>
AThe 'paged' parameter is missing from the query args, so it always fetches page 1
BThe 'posts_per_page' is too high, causing pagination to fail
CThe 'paginate_links' function is called without 'current' parameter, causing wrong page
DThe loop is missing wp_reset_postdata(), causing pagination to reset
Attempts:
2 left
💡 Hint

Check if the query knows which page to fetch.

state_output
advanced
1:30remaining
What is the value of $query->max_num_pages after this custom query runs?

Assuming there are 12 published posts, what is the value of $query->max_num_pages after this query?

Wordpress
<?php
$args = [
  'post_type' => 'post',
  'posts_per_page' => 5,
  'paged' => 1
];
$query = new WP_Query($args);
// What is $query->max_num_pages?
?>
A3
B2
C5
D12
Attempts:
2 left
💡 Hint

Divide total posts by posts per page and round up.

🧠 Conceptual
expert
2:30remaining
Which option correctly explains why 'paged' must be passed to WP_Query for pagination?

Why is it necessary to pass the 'paged' parameter to a custom WP_Query when implementing pagination?

A'paged' is only needed for default queries, not custom WP_Query instances
B'paged' tells WP_Query which page of results to fetch; without it, the query always returns the first page
C'paged' controls the number of posts per page; without it, no posts are returned
D'paged' enables WordPress to cache query results for faster pagination
Attempts:
2 left
💡 Hint

Think about how WordPress knows which posts to show on page 2, 3, etc.