612-293-8629 HELLO@THEMIGHTYMO.COM

Today I had a real problem with a WordPress website that uses multiple wp_query calls on the home page. The issue was that I could not use the next_posts_link() function properly, since WordPress ignores the “offset” parameter when creating paginated pages.

The solution I used was found here on the WordPress Codex.

Here is the code:

<?php
// Amount of posts in the first query
$qry[1] = 3;
// Amount of posts in the second query
$qry[2] = 6;
// Amount of posts in the third query
$qry[3] = 6;
// Total posts per page (sum of the 3 above values)
$qry['total'] = array_sum($qry);
// If paged is set then set the paged variable to that value, else it's 1 (the first page)
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
/*
	Numeric value to add to the offset if one is needed.
	page one - 0 (0x total = nothing)
	page two - 15 (total x1)
	page three - 30 (total x2)
	page four - 45 (total x3)
	..and so on..
*/
$onum = ($qry['total'] * ($paged - 1)); // $paged is a mutliplier
 
// If paged is more then 1 then set to the total of posts to show this page, else it's page one, so no offset
$offset = ($paged > 1) ? ($onum) : 0;
 
query_posts("posts_per_page=$qry[total]&paged=$paged&showposts=$qry[1]&offset=$offset");
if (have_posts()) :
	while (have_posts()) : the_post();
 
		// Loop stuff
 
	endwhile;
endif;
 
// Reuse the offset variable (rather then creating a new one), setting to a new value (plus any additional offset)
// Total in first query, plus any offset, if there is one
$offset = $qry[1] + (($paged > 1) ? $onum : 0);
 
query_posts("posts_per_page=$qry[total]&offset=$offset&showposts=$qry[2]");
if (have_posts()) :
	while (have_posts()) : the_post();
 
		// Loop stuff
 
	endwhile;
endif;
 
// Again just reusing the variable and adding offset (if page is more then 1)
// First posts total, plus second posts total, plus offset (if there is one)..
$offset = ($qry[1] + $qry[2]) + (($paged > 1) ? $onum : 0);
 
query_posts("posts_per_page=$qry[total]&offset=$offset&showposts=$qry[3]");
if (have_posts()) :
	while (have_posts()) : the_post();
 
		// Loop stuff
 
	endwhile;
endif;
 
// Unset the variables (we've finished with them, no reason to hold onto them)
unset($qry,$offset,$onum);
?>

Can you think of an easier way to do this?

PHONE, EMAIL, TEXT

We love to communicate: phone, email, text, Facebook, Twitter, LinkedIn, you name it.

LOCATIONS

PAYMENTS ACCEPTED

Call Now

Enroll in Our Free Beginner WordPress Class by Email

Never feel powerless in WordPress again!  Become a WordPress superstar for free in only minutes a day!

Our Beginner WordPress Email Class is delivered to your inbox!

Learn More 

You Have Successfully Enrolled! Prepare For Amazing!