1

I have a custom post type, jobs, being displayed on an external page using WP_Query() - the query itself is working fine.

However when I try to paginate the results, the older/newer links show up, but when I click on them, the content doesn't change - ie the page 'number' changes in the URL & the page reloads, but the posts don't move on.

I've tried using Wordpress' default next_posts_link & previous_posts_link and also with the wp_pagenavi plugin. In the code below I'm using wp_pagenavi .

Here's my code:

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $params = array( 'post_type' => 'job', 'post_status' => 'publish', 'posts_per_page' => 3, 'paged'=>$paged ); $temp = $wp_query; $wp_query= null; $wp_query = new WP_Query(); $wp_query->query($params); ?> <?php if (function_exists('wp_pagenavi')){ wp_pagenavi(); } ?> <?php if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?> // doing things with the post <?php endwhile; endif ?> <?php $wp_query = null; $wp_query = $temp; wp_reset_query() ?> 

Any help would be greatly appreciated it - this is driving me up the wall! ^_^

2
  • How many posts do you have in total? Commented Jan 9, 2012 at 22:27
  • 10 - I wish it was that simple ^_^ Commented Jan 9, 2012 at 22:28

3 Answers 3

1

the problem might be with the way you are using the global variable. Your code is manipulating the global $wp_query variable, which might be causing unexpected behavior. Instead of overwriting $wp_query, consider using a custom query variable. -OR- Implement Custom Query for Pagination:

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $params = array( 'post_type' => 'job', 'post_status' => 'publish', 'posts_per_page' => 3, 'paged' => $paged ); $job_query = new WP_Query($params); if ($job_query->have_posts()) : while ($job_query->have_posts()) : $job_query->the_post(); // Display your posts here endwhile; // Pagination if (function_exists('wp_pagenavi')) { wp_pagenavi(array('query' => $job_query)); } wp_reset_postdata(); // Reset post data after the custom query else : echo 'No jobs found'; endif; ?> 
-1

A problem with a custom query is that the posts_per_page resets before the page loads. So if you got 10 posts, than you only have one page, because the default posts_per_page is 10. If you have 11 posts than you will see the second page, but not beyond that.

Solution for this is changing your posts_per_page settings in the back-end: Settings > Reading > Blog pages show at most > 3. But this will change all archive page settings. Quick fix for this is adding

<?php global $query_string; query_posts($query_string . '&posts_per_page=10'); ?> 

at the top of your archive templates where you want the default posts_per_page.

-1

Try to use <?php wp_pagenavi(); ?> instead of <?php if (function_exists('wp_pagenavi')){ wp_pagenavi(); } ?> and if it is not working, try to add this code snippet

/** * Custom Pagination */ function custom_pagination($numpages = '', $pagerange = '', $paged='') { if (empty($pagerange)) { $pagerange = 5; } /** * This first part of our function is a fallback * for custom pagination inside a regular loop that * uses the global $paged and global $wp_query variables. * * It's good because we can now override default pagination * in our theme, and use this function in default quries * and custom queries. */ global $paged; if (empty($paged)) { $paged = 1; } if ($numpages == '') { global $wp_query; $numpages = $wp_query->max_num_pages; if(!$numpages) { $numpages = 1; } } global $wp_query; $big = 999999999; // need an unlikely integer $pages = paginate_links( array( 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), 'format' => '?paged=%#%', 'current' => max( 1, get_query_var('paged') ), 'total' => $numpages, 'prev_next' => false, 'type' => 'array', 'prev_next' => TRUE, 'prev_text' => __('<i class="fa fa-long-arrow-left"></i>'), 'next_text' => __('<i class="fa fa-long-arrow-right"></i>'), ) ); if( is_array( $pages ) ) { $paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged'); echo '<ul>'; foreach ( $pages as $page ) { echo "<li>$page</li>"; } echo '</ul>'; } } 

in your function.php and this code

<?php if (function_exists(custom_pagination)) { custom_pagination($the_query->max_num_pages,"",$paged); } ?> 

in a template file where you like to have the pagination.