6

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

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

And this is the function …

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
9

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 );
}
  • Thank you Sir. Perfect, but one last little thingy. I updated my question above with your code. 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 – mathiregister Sep 2 '12 at 6:59
  • See updated answer. I've added my entire function. Also: be careful with how you edit your question. If you overwrite your original code, it's difficult for later readers to follow the question-answer flow. It's usually better to retain the original code and then to add new code to the question. I'll edit your question to give you an idea what I mean. – Chip Bennett Sep 2 '12 at 14:08
  • 1
    To address your specific follow-up question, refer to the $pagination['base'] ternary near the end of the function. I think you should be able to adapt that line to suit your needs. – Chip Bennett Sep 2 '12 at 14:16
  • Thank you, However I seem to have some problem elsewhere. When using your code the pagination won't work again - exact same behaviour as mentioned in my question above. The link clicked doesn't update the view and the pagination doesn't work. If I uncomment your last two if blocks in your function (the lines regarding the pemalinks) your function works fine again. So everything works fine except for the part with the rewriting of ?paged in the url itself. Any other ideas where that could come from? Thanks again for your awesome help so far. – mathiregister Sep 2 '12 at 14:55
  • I suspect the problem is your custom rewrite rules. That's the part of my function that you've changed... – Chip Bennett Sep 3 '12 at 18:23
0

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

  • Thank you for your response. It's not the get_template_part() because I've also tried inserting the code of the template directly into the loop. And the pagination technique you posted in your comment doesn't make a difference - same effect. – mathiregister Sep 1 '12 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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