Recently I’ve created a microblog within my site to post simple and interesting things I’ve come across on the net that don’t fit into my current blog structure. Technically speaking, it’s just a separate Wordpress page modified with a custom template and query to grab posts only from a specific category. Once I pulled the relevant posted from the database, the goal was to display the latest ten on the page, and paginate the rest. Simple enough.
However I ran into a problem once Wordpress was supposed to start paging content for me. The microblog page was still only showing my latest ten posts, which was desired behavior, but when I clicked the link to ‘page 2′ of the content, Wordpress would re-display the latest ten post. The paging was not working.
After a little digging I found the culprit to be the the need of a $wp_query paging global variable. This variable is part of the $wp_query object, which Wordpress uses by default within it’s main loops, but isn’t available when you create a custom instance of $wp_query or (in my case) use the query_posts() function.
Thankfully this is a simple fix. Making use of the Wordpress function $get_query_var, you’re allowed to query the $wp_query object and retrieve the global variable. Assigning this value to a variable of the same name, and then referencing it in your post query was all Wordpress needed to pass the global value to the custom query object. My instantiation code ended up looking like this:
/*
Wordpress Custom Query Paging Correction
*/
<?php
// If the paging global doesn't yet exists: set it to 1
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('cat=482&paged='.$paged);
?>