Queries and pagination

Published on: January 9, 2012 | Categories: Tutorial

A user was recently having some difficulties working with a custom query he created using the ‘tribe_get_events()‘ function and the WP-PageNavi plugin. No matter what he tried, the paging wouldn’t work right. The following quick tutorial explains how to query properly while having proper pagination.

Warning: some understanding of PHP is required :)

The WP-PageNavi (or any kind of paging for that matter) will not work with the ‘tribe_get_events()’ function. The ‘tribe_get_events()’ function uses the WordPress ‘get_posts()‘ function behind the scenes, which by its nature (a function to return an array of posts, not used for any kind of main query) doesn’t do paging. In fact, the only way to obtain paging with ‘get_posts()’ would be to make a multiplication and use the ‘offset’ query argument, an example and explanation of which is provided at WP Engineer.

The ‘offset’ approach is definitely more of a hack and less of a solution when it comes to having paging with your queries. A much cleaner and better solution is to use the ‘WP_Query‘ WordPress object to make the query. In fact, queries written with the ‘WP_Query’ object are often more efficient and perform better as opposed to queries with ‘get_posts’ or ‘query_posts’ – I recommend you use it for all your custom queries if you can!

Below you have the original code from the user and our improved code using ‘WP_Query’

<h1>Upcoming events:</h1>
<?php global $post;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$upcoming = tribe_get_events( array(
	'eventDisplay'=>'past',
	'posts_per_page'=>1,
	'paged' => get_query_var('page')
	) );

foreach($upcoming as $post) :
	setup_postdata($post); ?>
	<div class="single-event">
		<div class="title">
			<a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
		 </div>
	</div>
<?php
endforeach;
if ( function_exists('wp_pagenavi') ) wp_pagenavi();

/**
 * above is the original untouched code
 * below is the better code (and it works!)
 * the main difference is the use of WP_Query instead of tribe_get_events
 * doing so is more efficient and ensures compatibility with WPPageNavi
 * @see @link http://scribu.net/wordpress/wp-pagenavi/wpn-2-74.html for more details
 */

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$upcoming = new WP_Query();
$upcoming->query( array(
	'post_type'=> 'tribe_events',
	'eventDisplay' => 'past',
	'posts_per_page' => 1,
	'paged' => $paged),
);

if ($upcoming->have_posts()) :
	while ($upcoming->have_posts()) :
		$upcoming->the_post();
		?>
		<div class="single-event">
			<div class="title">
				<a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
			</div>
		</DIV>
	<?php
	endwhile;
	if ( function_exists('wp_pagenavi') ) wp_pagenavi( array( 'query' => $upcoming ) ); // tell it which query we are paginating
endif;
wp_reset_query(); // important to reset the query
?>

4 Responses to Queries and pagination

  1. Dave says

    Would be nice to always show a screenshot of the outcome, this would encourage users to try and implement these hacks.

    • Rob La Gatta says

      That is a great tip, Dave. I’ve spread the word to the rest of the folks on the team so we can all make a point of starting to include screenshots on tutorials/’how to’ posts down the road. Thanks for the heads up.

  2. brad says

    I believe there needs to be another closing round bracket on line 36

  3. brad says

    (edit) nope, i’m wrong, but i there is still an error: the extra comma in line 35