I can't seem to get pagination to work correctly in my search results template. The page links appear, but when i click onto page 2, it gives me a 404 error.
Search.php
<?php
global $wp_query;
$total_results = $wp_query->found_posts;
?>
<?php
if(!$wp_query) global $wp_query;
$search_refer = $_GET["post_type"];
if ($search_refer == 'archive') { load_template(TEMPLATEPATH . '/search-gallery.php');}
else{load_template(TEMPLATEPATH . '/search-standard.php');}
?>
Pagination Code in Functions.php
function pagination($pages = '', $range = 4){
$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\"><span>Page ".$paged." of ".$pages."</span>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>« First</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹ Previous</a>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $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)."\">Next ›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last »</a>";
echo "</div>\n";
}
}
Search Results Template
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$search_query = array(
'posts_per_page' => '10',
'paged' => $paged,
);
$wp_query = new WP_Query($search_query);
if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="courseContent coursesLayout alumniMainContent alumniSpacing newsBoxMore">
<div class="courseContentHolder alumniContentHolder newsBoxMainHolder">
<div class="newsMainImage">
<?php the_post_thumbnail(); ?>
</div>
<div class="alumniHeading newsMainHeading">
<h5><?php the_title();?></h5>
</div>
<div class="alumniContent newsMainContent">
<?php the_excerpt(); ?>
</div>
</div>
<span class="readmoreButton" style="display: inline !important;"><a title="Student Awards" href="<?php the_permalink();?>">Read More</a></span>
</div><?php edit_post_link('Edit'); ?>
<?php endwhile; ?><?php else : ?>
<p class="notfoundtext">Sorry, nothing came back matching what you searched. Try again?</p>
<?php endif; ?>
<?php pagination(); ?>
Any suggestions?