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! ^_^
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
at the top of your archive templates where you want the default posts_per_page.
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 snippetin your function.php
and this code
in a template file where you like to have the pagination.