Jepson Center for the Arts

How to Paginate a Custom WordPress Query

On a recent project for a client, I needed a way to have a WordPress Page contain both static content editable by the client, and dynamic content added/updated every time they made a new Post. Additionally, the dynamic content had to be paginated.

Ultimately the goal was to a have a Page with introductory text that could be edited by the client but that also served up a list of posts tagged with a certain tag. Like so:

AV12-donor

Initially the concept was easy enough—create a new Page with multiple loops. The first loop would be the main query that pulled the static content while a secondary, custom loop query pulled posts with a specific tag.

The challenge lay in the fact that the static content would always be one page long, whereas the dynamic content might eventually grow into a really long list of posts that would need to be paginated.

So how do we go about creating a paging system that would move through the dynamically-generated list of posts? By re-defining the $wp_query object.

The $wp_query Object

$wp_query is the global variable that receives the parameters for a WordPress Page/Post query. Once it gets this information, it attempts to pull the appropriate content from your data base. If you’re calling a WordPress Page, it will show the content from the Page (the text/images you enter in the Page editor when you create a new Page). If you’re calling a Post, it will pull the information for that post (or posts).

$wp_query receives this information from the WP_Query Class (WP v2.0+). The Codex Page on the WP_Query Class goes into detail on how the WP_Query Class functions, but the short of it is that this Class is the magic that makes the content appear on your blog, whether that content is in a Post or in a Page.

Continues…

Snapshot: Post

Posted: August 18th, 2009

In this article we'll discuss an easy way to paginate a custom WordPress post query on a given page.

Disclaimer: While this script may come in handy for others, I designed it specifically for use on this site. I haven’t tested it in any version of Wordpress other than those that I’ve used (v2.6.3–v2.8.6). Feel free to use it if you’d like, but I make no guarantee it will work with your current theme or Wordpress setup. For a complete Copyright Notice and Disclaimer, visit the About Section of this site.

Topics

Or you can check out the full archive here: Article Archive.

Search