0

Common problem, yet this time I can't seem to figure it out: the pagination at my search.php is not working, it sends me to the index page the moment I click on "page #"

My search.php is set up like described by wordpress http://codex.wordpress.org/Creating_a_Search_Page :

 <?php
global $query_string;
$query_args = explode("&", $query_string);
$search_query = array();
foreach($query_args as $key => $string) {
    $query_split = explode("=", $string);
    $search_query[$query_split[0]] = urldecode($query_split[1]);
} 
$search = new WP_Query($search_query);
$total_results = $search->found_posts;

if ( $search->have_posts()) : 
    echo $total_results;
    wp_page_numbers();

while ($search->have_posts()) : $search->the_post();
    get_template_part( 'content', get_post_format() );
endwhile; ?>

Then the pagination function in functions.php:

function wp_search_numbers() {
global $wp_query, $wp_rewrite;
$wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;

$pagination = array(
    'base' => @add_query_arg('paged','%#%'),
    'format' => '',
    'total' => $wp_query->max_num_pages,
    'current' => $current,
    'show_all' => true,
    'type' => 'list',
    'next_text' => '&raquo;',
    'prev_text' => '&laquo;'
    );

if( $wp_rewrite->using_permalinks() )
    $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );

if( !empty($wp_query->query_vars['s']) )
    $pagination['add_args'] = array( 's' => get_query_var( 's' ) );

echo paginate_links( $pagination );
}

The pagination on index, taxonomy and other custom pages work fine. I have tried several things, like adding: // Pagination fix $temp_query = $wp_query; $wp_query = NULL; $wp_query = $custom_query; But that changes my search results to zero. Also tried resetting the permalinks, which didn't change anything. And I removed the $wp_query = new WP_Query(); and went for the general query_posts( $args ); without any change.

The strangest thing is that I just found out, it's not for all the numbers in the pagination. If I do a search query with a results of 15 pages: Page 3 and 8 - 15 work, but not page 2 - 5.

Could anyone tell me in what direction I should look for the error?

  • Did you search the site for related questions and answers? – Chip Bennett Nov 14 '13 at 17:31
  • Hi Chip, yes, for many days... – inTOWN Nov 14 '13 at 17:35
  • Can you please edit your question to clarify what you're attempting to do? Your code looks like a search results query, but your Codex link is for a custom page template for conducting a search. Also, if you attempt code, please edit your question to include the new code, describe what you expected to happen that didn't, or what happened unexpectedly. – Chip Bennett Nov 14 '13 at 17:51

Browse other questions tagged or ask your own question.