1

I have allow readers to select the posts order with different parameters. For a example users can order posts by "vote". (Vote is a custom post type.)

Then URL will be http://example.com/?sort_by_type=vote

I have used pre_get_posts action to do orderthe posts. It works fine. The problem is pagination .

Pagination look like this in my theme.

http://example.com/?sort_by_type=vote/page/2/

But it should be http://example.com/page/2/?sort_by_type=vote to work correctly.

So how I correct pagination?


Edit After comment of @govind : The theme which I used is a not a theme developed by me. What I did i, if URL contain ?sort_by_type=vote request I changed the post order using pre_get_posts filter.

When I cheeking theme I found following code.

<?php if ( is_home() || is_archive() || is_search() ) : // If viewing the blog, an archive, or search results. ?>

    <?php loop_pagination(
        array( 
            'prev_text' => _x( '&larr; Previous', 'posts navigation', 'daily' ), 
            'next_text' => _x( 'Next &rarr;',     'posts navigation', 'daily' )
        ) 
    ); ?>

<?php endif; ?>
2
  • what function did you use to get the website current page url ? please show me your code.
    – GKS
    Commented Dec 10, 2016 at 6:01
  • @GovindKumar I edited the question.
    – Ranuka
    Commented Dec 10, 2016 at 6:10

2 Answers 2

2

You can create your custom pagination using paginate_link() function and add custom query string after current url.

To add argument to the links of paginations you can pass them as array argument inside the 'add_args'

if ( is_home() || is_archive() || is_search() ) : 

    echo paginate_links(array(
         'base' => preg_replace('/\?.*/', '/', get_pagenum_link(1)) . '%_%',
         'current' => max(1, get_query_var('paged')),
         'format' => 'page/%#%',
         'total' => $wp_query->max_num_pages,
         // here you can pass custom query string to the pagination url
         'add_args' => array(
             'sort_by_type' => ( !empty($_GET['sort_by_type']) ) ? $_GET['sort_by_type'] : 'vote'
         )
     ));    

endif; 

Replace your pagination code with this above code.

It will print all string with custom query string. http://example.com/page/2/?sort_by_type=vote

Hope this help!

0

You should define a new pagination function for do it.

<?php
function custom_pagination() {

    if( is_singular() )
        return;

    global $wp_query;

    /** Stop execution if there's only 1 page */
    if( $wp_query->max_num_pages <= 1 )
        return;

    $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
    $max   = intval( $wp_query->max_num_pages );

    /** Add current page to the array */
    if ( $paged >= 1 )
        $links[] = $paged;

    /** Add the pages around the current page to the array */
    if ( $paged >= 3 ) {
        $links[] = $paged - 1;
        $links[] = $paged - 2;
    }

    if ( ( $paged + 2 ) <= $max ) {
        $links[] = $paged + 2;
        $links[] = $paged + 1;
    }

    echo '<div class="navigation"><ul>' . "\n";

    /** Previous Post Link */
    if ( get_previous_posts_link() )
        printf( '<li>%s</li>' . "\n", get_previous_posts_link() );

    /** Link to first page, plus ellipses if necessary */
    if ( ! in_array( 1, $links ) ) {
        $class = 1 == $paged ? ' class="active"' : '';

        printf( '<li%s><a href="%s">%s?sort_by_type=vote</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );

        if ( ! in_array( 2, $links ) )
            echo '<li>…</li>';
    }

    /** Link to current page, plus 2 pages in either direction if necessary */
    sort( $links );
    foreach ( (array) $links as $link ) {
        $class = $paged == $link ? ' class="active"' : '';
        printf( '<li%s><a href="%s">%s?sort_by_type=vote</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
    }

    /** Link to last page, plus ellipses if necessary */
    if ( ! in_array( $max, $links ) ) {
        if ( ! in_array( $max - 1, $links ) )
            echo '<li>…</li>' . "\n";

        $class = $paged == $max ? ' class="active"' : '';
        printf( '<li%s><a href="%s">%s?sort_by_type=vote</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
    }

    /** Next Post Link */
    if ( get_next_posts_link() )
        printf( '<li>%s</li>' . "\n", get_next_posts_link() );

    echo '</ul></div>' . "\n";

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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