Bird
0
0

Examine this pagination code snippet:

medium📝 Debug Q6 of 15
Wordpress - WordPress Query and Database
Examine this pagination code snippet:
$current_page = get_query_var('paged');
$args = ['posts_per_page' => 5, 'paged' => $current_page];
$query = new WP_Query($args);

What is the potential issue with this code?
AIf 'paged' is not set, $current_page will be empty, causing pagination to fail.
BUsing 'paged' instead of 'page' causes the query to ignore pagination.
CThe 'posts_per_page' parameter must be set to 10 for pagination to work.
DWP_Query does not support the 'paged' parameter in custom queries.
Step-by-Step Solution
Solution:
  1. Step 1: Check get_query_var('paged')

    If the 'paged' query variable is not set in the URL, get_query_var('paged') returns an empty value.
  2. Step 2: Default to page 1

    To avoid issues, you should default $current_page to 1 when 'paged' is empty, e.g., $current_page = get_query_var('paged') ? get_query_var('paged') : 1;.
  3. Final Answer:

    If 'paged' is empty, pagination will not work correctly. -> Option A
  4. Quick Check:

    Always set a default page number [OK]
Quick Trick: Always default 'paged' to 1 if empty [OK]
Common Mistakes:
  • Assuming get_query_var('paged') always returns a number
  • Using 'page' instead of 'paged' for pagination
  • Not setting posts_per_page parameter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Wordpress Quizzes