Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This question already has an answer here:

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)."'>&laquo; First</a>";
     if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; 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 &rsaquo;</a>";  
     if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</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?

share|improve this question

marked as duplicate by Chip Bennett, toscho Oct 31 '13 at 15:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers 2

Page 1 on repeat is usually a wp_query issue. A 404 is a permalink issue - try setting them to default and see if the same problem occurs. If it doesn't, then switch them back to custom and your problem should have resolved itself.

If default permalinks give you a 404 on page two then we need to look at that pagination code a bit more closely. Personally, I'm a fan of Justin Tadlock's code because it's flawless.

https://github.com/justintadlock/hybrid-core/blob/master/extensions/loop-pagination.php

share|improve this answer
    
I changed the permalink structure back to standard and it did the same thing, 404 issue, then i tried Justin Tadlock's code but it exhibits strange behaviour, it has page 1 on repeat and page 1 on the links section is highlighted no matter what page I am on! –  RoseCoder Jul 18 '13 at 10:24

You are overwriting $wp_query inside the search template. That means that the main query that runs before your search template loads is going to be out or sync with the $wp_query in the template. That tends to cause 404s.

Since the only thing you are doing is changing the posts_per_page a very simple pre_get_posts callback will fix at least part of this.

function alter_search_ppp_wpse_106961($qry) {
  if ($qry->is_main_query() && $qry->is_search()) {
    $qry->set('post_per_page',10);
  }
}
add_action('pre_get_posts','alter_search_ppp_wpse_106961');

With that is place you do not need, and should remove, this:

$search_query = array(
    'posts_per_page' => '10',
    'paged' => $paged,
    );
$wp_query = new WP_Query($search_query);

I don't know why you've rolled your own pagination, and I can't tell by looking at it if that code is going to generate problem URLs, but I'd suggest you look into paginate_links

share|improve this answer
    
Thank you, I thought it might had been overwriting the $wp_query. I tried the pre_get_posts but it didn't do anything –  RoseCoder Jul 19 '13 at 7:35
    
"it didn't do anything" is not very descriptive. –  s_ha_dum Jul 19 '13 at 13:35
    
The code that you gave caused no change in regards to making the pagination worked. When i performed a search, it did not limit it to 10 posts per page, it showed all. I tried removing my custom pagination and adding paginate_links and it did nothing. Is that better? –  RoseCoder Jul 19 '13 at 18:11
    
What is the name of your search template .php file? –  s_ha_dum Jul 19 '13 at 18:22
    
I have search.php which contains the code in my original question, which directs to two other templates and the one i want the pagination to work on is named search-standard.php –  RoseCoder Jul 19 '13 at 18:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.