Support » Fixing WordPress » Posts pagination. How is the query made available to page 2, 3, 4 and so on

  • Can anybody point me towards information about how pagination works? I don’t mean how paginate_links() outputs the URLs, I mean how does WP make the post information (the query) available to each page /2/3/4/5 and so on.

Viewing 3 replies - 1 through 3 (of 3 total)
  • wpismypuppet


    Well, I don’t know of any documentation that would explain pagination… But I can tell you how it works (in an over simplified way, of course. SQL is very powerful and too difficult to explain in a forum).

    All WordPress pages/posts/attachments/whatever are loaded through a database. The URL in the address bar contains information that is run through SQL statements to get information from the database. Even if you have “pretty” URLs, the underlying “real” URL contains query string data.

    When you request a page, a SQL statement pulls ids and other query information from the URL and pushes it through a SQL query. This is how it knows what post type and ID to show you. Once the SQL is run, it goes through a hierarchy of page “templates” (or physical pages such as index.php or page.php) to display the database information.

    When you paginate, you are passing a “page number” variable in the URL. The SQL statement uses that variable to know which IDs to get based on a “start” and “end” value. It “fakes” pagination by only showing a certain number of returned SQL table rows. Again, the variable in the URL tells it what to do.

    When you see page /2/3/4/5 and so on, you are looking at the same “physical” page (index.php or page.php, etc.) and it’s displaying a different SQL query based on the variable it finds in the URL.

    Hope that sheds some light.

    @wordpressismypuppet thanks for the very thorough explanation, very much appreciated!



    Anytime… glad I could help!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Posts pagination. How is the query made available to page 2, 3, 4 and so on’ is closed to new replies.