Bird
0
0

You need to display a paginated list of 'product' custom post types with 6 posts per page. Which code snippet correctly implements this with proper pagination?

hard📝 component behavior Q8 of 15
Wordpress - WordPress Query and Database
You need to display a paginated list of 'product' custom post types with 6 posts per page. Which code snippet correctly implements this with proper pagination?
A<pre>$paged = get_query_var('paged'); $args = [ 'post_type' => 'product', 'posts_per_page' => 6, 'page' => $paged ]; $query = new WP_Query($args);</pre>
B<pre>$paged = get_query_var('page'); $args = [ 'post_type' => 'product', 'posts_per_page' => 6 ]; $query = new WP_Query($args);</pre>
C<pre>$args = [ 'post_type' => 'product', 'posts_per_page' => 6, 'paged' => 1 ]; $query = new WP_Query($args);</pre>
D<pre>$paged = max(1, get_query_var('paged')); $args = [ 'post_type' => 'product', 'posts_per_page' => 6, 'paged' => $paged ]; $query = new WP_Query($args);</pre>
Step-by-Step Solution
Solution:
  1. Step 1: Retrieve current page

    Use get_query_var('paged') and default to 1 if empty using max(1, get_query_var('paged')).
  2. Step 2: Set query arguments

    Include 'post_type' => 'product', 'posts_per_page' => 6, and 'paged' => $paged.
  3. Step 3: Instantiate WP_Query

    Pass the $args array to WP_Query to enable pagination.
  4. Final Answer:

    $paged = max(1, get_query_var('paged'));
    $args = [
      'post_type' => 'product',
      'posts_per_page' => 6,
      'paged' => $paged
    ];
    $query = new WP_Query($args);
    correctly sets up pagination for 'product' posts.
  5. Quick Check:

    Use 'paged' with default 1 and correct post_type [OK]
Quick Trick: Use max(1, get_query_var('paged')) for current page [OK]
Common Mistakes:
  • Using 'page' instead of 'paged' parameter
  • Not defaulting paged to 1
  • Omitting 'paged' parameter entirely

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Wordpress Quizzes