I have a function that works fine for pagination on a search page:
function pagination($pages = '', $range = 2)
{
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo "<div class='pagination'>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>«</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹</a>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+5 || $i <= $paged-$range-5) || $pages <= $showitems ))
{
echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
}
}
if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>»</a>";
echo "</div>
";
}
}
And I call it using this code after the wp_query
:
<?php pagination($pages = $wp_query->max_num_pages); ?>
The wp_query
has paged' => $paged,
in the arguments.
But, when I try and paginate the homepage the pagination doesn't actually work. It'll link to the correct URL, but the posts don't actually change.
The home page has been made a page template (not using index.php) and in settings the front page has been set to the correct page.
But I have no idea why it's not paginating on the homepage but is on the search page. If I set the search page to the front page in WP Settings that pagination also breaks.
[EDIT] (code for query, as requested below)
$args = array(
// general
'post__in' => $postIDs,
'post_type' => 'event',
'posts_per_page' => 10,
'paged' => $paged,
'meta_key' => $_SESSION['search']['sort-key'],
'orderby' => $_SESSION['search']['sort-by'],
'order' => 'ASC',
// what input
'title_like' => $_SESSION['search']['keyword'],
// change to sub categories/ keywords
// category filter
'tax_query' => array(
array(
'taxonomy' => 'main-cat',
'field' => 'slug',
'terms' => $mainCat
)
// add sub category search too
),
// date filter
'meta_query' => array(
array(
'key' => 'date_%_start-date',
'value' => $when,
'compare' => '>=',
'type' => 'NUMERIC'
),
array (
'key' => 'date_%_end-date',
'value' => $when2,
'compare' => '<=',
'type' => 'NUMERIC'
)
)
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( $args );
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div class="event clearfix">
<div class="event-image grid-1-4 no-padding">
<?php if( get_field('images') ) : ?>
<?php $images = get_field('images'); ?>
<img src="<?php echo $images[0][sizes][thumbnail]; ?>" alt="<?php the_title(); ?>" />
<?php else : ?>
<br />
<?php endif; ?>
</div>
<div class="event-info grid-1-2 clearfix">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php include('date.php'); ?>
<?php
$minidescription = get_field('mini-description');
$minidescription = html_entity_decode($minidescription);
echo '<p>' . $minidescription . '</p>';
?>
</div>
<div class="event-details grid-1-4 no-padding">
<div class="detail">Distance <?php echo round($post->distance, 1); ?> mi</div>
<?php if( get_field('adult') == '' || get_field('adult') == '0' ) : ?>
<div class="detail">Price: Free</div>
<?php else : ?>
<div class="detail">Price: £<?php the_field('adult'); ?></div>
<?php endif; ?>
<?php include('countdown.php'); ?>
</div>
</div>
<?php $plus++; ?>
<?php endwhile; ?>
<div class="grid-100">
<?php pagination(); ?>
</div>