0
0
Wordpressframework~10 mins

Pagination with custom queries in Wordpress - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Pagination with custom queries
Start: User visits page
Set current page number
Create custom query with page number
Run query to get posts
Display posts for current page
Show pagination links
User clicks next/prev page
Set current page number
This flow shows how WordPress uses a custom query with a page number to fetch and display posts, then shows pagination links to navigate pages.
Execution Sample
Wordpress
<?php
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = ['post_type' => 'post', 'paged' => $paged, 'posts_per_page' => 2];
$query = new WP_Query($args);
while ($query->have_posts()) { $query->the_post(); the_title(); }
?>
This code gets the current page number, runs a custom query for posts with pagination, and displays post titles.
Execution Table
StepActionPaged ValueQuery ArgsPosts RetrievedOutput
1Get current page number1N/AN/Apaged = 1
2Create query args1{post_type: 'post', paged: 1, posts_per_page: 2}N/AArgs ready
3Run WP_Query1{post_type: 'post', paged: 1, posts_per_page: 2}Posts 1 & 2Posts 1 & 2 titles shown
4Display pagination links1N/AN/ALinks: Prev (disabled), Next (page 2)
5User clicks Next2N/AN/Apaged updated to 2
6Create query args2{post_type: 'post', paged: 2, posts_per_page: 2}N/AArgs ready
7Run WP_Query2{post_type: 'post', paged: 2, posts_per_page: 2}Posts 3 & 4Posts 3 & 4 titles shown
8Display pagination links2N/AN/ALinks: Prev (page 1), Next (page 3)
9User clicks Next3N/AN/Apaged updated to 3
10Create query args3{post_type: 'post', paged: 3, posts_per_page: 2}N/AArgs ready
11Run WP_Query3{post_type: 'post', paged: 3, posts_per_page: 2}Post 5 onlyPost 5 title shown
12Display pagination links3N/AN/ALinks: Prev (page 2), Next (disabled)
13User clicks Next4N/AN/ANo posts, end reached
14Exit4N/ANoneNo posts to show, pagination ends
💡 Paged 4 has no posts, so pagination stops.
Variable Tracker
VariableStartAfter Step 1After Step 5After Step 9After Step 13
pagedN/A1234
query argsN/A{post_type:'post',paged:1,posts_per_page:2}{post_type:'post',paged:2,posts_per_page:2}{post_type:'post',paged:3,posts_per_page:2}{post_type:'post',paged:4,posts_per_page:2}
posts retrievedN/APosts 1 & 2Posts 3 & 4Post 5None
Key Moments - 3 Insights
Why does the 'paged' variable start at 1 and not 0?
Because WordPress pagination pages start counting from 1, not 0. See execution_table step 1 where paged is set to 1.
What happens when the user clicks 'Next' on the last page?
No posts are retrieved and pagination stops. See execution_table steps 13 and 14 where paged is 4 but no posts exist.
Why do we pass 'paged' in the query args?
To tell WP_Query which page of posts to fetch. Without it, the query always returns the first page. See execution_table steps 2, 6, and 10.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What posts are retrieved?
APosts 3 & 4
BPosts 1 & 2
CPost 5 only
DNo posts
💡 Hint
Check the 'Posts Retrieved' column at step 3 in execution_table.
At which step does the 'paged' variable change from 2 to 3?
AStep 5
BStep 13
CStep 9
DStep 1
💡 Hint
Look at variable_tracker for 'paged' values after steps.
If 'posts_per_page' was changed to 3, how would the posts retrieved at step 3 change?
APosts 1, 2, and 3
BPosts 1 & 2 only
CPosts 3 & 4
DNo posts
💡 Hint
Posts retrieved depend on 'posts_per_page' in query args at step 3.
Concept Snapshot
Pagination with custom queries in WordPress:
- Use get_query_var('paged') to get current page (default 1).
- Pass 'paged' and 'posts_per_page' in WP_Query args.
- Loop through posts with have_posts() and the_post().
- Show pagination links to navigate pages.
- Clicking links updates 'paged' and reruns query.
Full Transcript
This visual execution shows how WordPress handles pagination with custom queries. First, it gets the current page number using get_query_var('paged'), defaulting to 1 if none is set. Then it creates query arguments including 'paged' and 'posts_per_page' to control which posts to fetch. WP_Query runs with these args to get posts for that page. The posts are displayed, and pagination links are shown to let users move to next or previous pages. When a user clicks a pagination link, the 'paged' variable updates, and the query runs again for the new page. This repeats until no more posts are found, ending pagination. The execution table traces each step, showing variable changes and posts retrieved. Key moments clarify why 'paged' starts at 1, why it is passed in query args, and what happens at the last page. The quiz tests understanding of posts retrieved per page and variable changes. This helps beginners see how pagination works step-by-step in WordPress custom queries.