• Hello!

    Some info:
    1. I’m using a custom build wp_query to display a list of custom posts with some custom post fields. Works great.

    2. I’m using the following code to sort the posts ASC/DESC depending on custom fields. Simple url GET with custom keys and values. Also works fine.

    <a href="<?php echo esc_attr( add_query_arg( 'order', 'city_desc' ) ); ?>"></a>  
    
    if (isset($_GET['order'])) {
    $sort= $_GET['order'];
    
     if($sort == 'city_desc'){
                      $args['order'] = 'DESC'; $args['meta_key'] = 'city';
    				  }

    The problem
    When I enable pagination and sort the results – everything seems fine at first: the order is ok, the posts get split ect. But when I enter the second page of results something strange happens.

    Let’s say I have 3 posts and a custom field ‘price’:

    POST A – 100
    POST B – 200
    POST C – 300
    Posts per page is set to 2.

    Pagination page #1 after sorting DESC I get:

    POST C – 300
    POST B – 200

    But on page #2 I get:

    POST C – 300
    and that’s it

    So it seems there is a conflict between the pagination function which returns a given number of posts (1 in this example) on the 2nd page, but uses the sorting, resulting in a duplicate of POST C.

    Here are the URL for those pages:

    For sorted results:
    list_page/?order=price_desc

    For page #2

    some_page/page/2/?order=price_desc%2F

    I’m using a plugin to do the pagination.

    Any thoughts? 🙂

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    It’s hard to say without knowing what the plugin is doing, but your are correct, there is a conflict with the plugin because it’s apparently assuming an ASC sort order. You might be able to hack the plugin to recognize the DESC order, but from a practical standpoint it’s best if the pagination is handled in the same query so everything remains in synch.

    Pagination is not really that complicated, the offset parameter in the LIMIT clause is simply adjusted to [(page-1)*posts_per_page]+1.

    Another possibility since you are using a custom WP_Query object is the plugin is pulling pagination information from the main query and applying it to your query, a complete mismatch of where the user is. Again, managing the pagination directly in your custom query would solve the issue.

Viewing 1 replies (of 1 total)
  • The topic ‘Pagination problem on custom wp_query sort order’ is closed to new replies.