paginate_links() don’t properly work in search.php?

I’m using this in my search.php template …

<div class="pagination">
    <?php echo get_pagination_links(); ?>
</div>

And this is the function …

Read More
function get_pagination_links() {
    global $wp_query;
    $big = 999999999;

    return paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $wp_query->max_num_pages,
        'prev_next'    => false
    ) );
}

The output works: So if I search for “test” and there are more then 10 results and the pagination shows 1 2 where 2 is a link. When clicking page 2 the address bar correctly updates to mysite.com/search/test/page/2 but the resulting page is still number 1.

There are still the same results as on page number 1 and the pagination links are still the same – 2 is the still the link and 1 is the current page.

Any ideas why that could happen?

Update

Here is where the custom function is called in the template:

<?php
/**
 * The template for displaying Search Results pages
 */

get_header(); ?>

<?php if ( have_posts() ) : ?>
    <hgroup class="section-heading wrapper">
        <h1>Results</h1>
    </hgroup>

    <section id="results">
        <?php while ( have_posts() ) : the_post(); ?>
            <ul class="event-items">
                <?php get_template_part( 'inc/search', 'result' ); ?>
            </ul>
        <?php endwhile; ?>
    </section>

    <div class="pagination tiny wrapper">
        <?php echo get_pagination_links(); ?>
    </div>

    <?php else : ?>
        <div class="wrapper">
            <h2>Nothing found</h2>
        </div>
<?php endif; ?>

<?php get_footer(); ?>

Edit

Using recommended code from the answer below:

/**
 * Pagination links for search and archives
 */

function get_pagination_links() {
    global $wp_query;
    $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
    $big = 999999999;

    return paginate_links( array(
        'base' => @add_query_arg('paged','%#%'),
        'format' => '?paged=%#%',
        'current' => $current,
        'total' => $wp_query->max_num_pages,
        'prev_next'    => false
    ) );
}

The pagination is working now however the “permalinks” are not exactly what I want them to be. I use custom_pagination_base() to have the this permalink structure:

  • mypage.com/search/term/seite/2 (where seite is the german word for page).

Is it possible to keep the permalinks working like that. The paginaton works with your code, just the permalinks look like this:

  • mypage.com/search/term?paged=2

Related posts

Leave a Reply

2 comments

  1. I’m fairly certain this is answered elsewhere, but I’ll add it here again.

    I believe your issue lies here:

    'current' => max( 1, get_query_var('paged') ),
    

    Try this instead:

    global $wp_query;
    $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
    

    …then:

    'current' => $current;
    

    Your 'base' may also be an issue. Instead of this:

    $big = 999999999;
    //...
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) )
    

    I use this:

    'base' => @add_query_arg('paged','%#%')
    

    Edit

    In case it’s helpful, here’s my entire paginate_links() wrapper function:

    /**
     * Paginate Archive Index Page Links
     */
    function oenology_get_paginate_archive_page_links( $type = 'plain', $endsize = 1, $midsize = 1 ) {
        global $wp_query, $wp_rewrite;  
        $current = get_query_var( 'paged' ) > 1 ? get_query_var('paged') : 1;
    
        // Sanitize input argument values
        if ( ! in_array( $type, array( 'plain', 'list', 'array' ) ) ) $type = 'plain';
        $endsize = absint( $endsize );
        $midsize = absint( $midsize );
    
        // Setup argument array for paginate_links()
        $pagination = array(
            'base'      => @add_query_arg( 'paged', '%#%' ),
            'format'    => '',
            'total'     => $wp_query->max_num_pages,
            'current'   => $current,
            'show_all'  => false,
            'end_size'  => $endsize,
            'mid_size'  => $midsize,
            'type'      => $type,
            'prev_text' => '&lt;&lt;',
            'next_text' => '&gt;&gt;'
        );
    
        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' ) );
    
        return paginate_links( $pagination );
    }
    
  2. Although this isn’t a solution using your exact code perhaps you can try using this pagination technique as opposed to the example you have used from the WordPress Codec

    At a guess I’d say the issue lies with you getting the template part with this line of code:

    <?php get_template_part( 'inc/search', 'result' ); ?>
    

    If this doesn’t solve your issue I’d look at bringing the content from ‘inc/search’ and include it in your search results page without using <?php get_template_part(); ?> – it might help you spot any oddities