AJAX pagination offset returns same post on adjacent pages

I have a simple AJAX pagination that seemed to work just fine, calling one WordPress page (not ideal, I know) with a pagenum and a sort order arguments and it returned 4 custom posts per call. The problem I found while testing it is that if the items are ordered like this in the db:

item1: 4 votes
item2: 3 votes
item3: 3 votes
item4: 3 votes
----page2----
item5: 3 votes
item6: 2 votes
...

…there’s a chance that any one of items with 3 votes in custom field will display as the first item on page2 of the pagination. This effectively erases one of the items from listing, and I can’t seem to find a way to query get_posts properly.

Read More

This is how it looks now:

elseif($savjet_order == 'votes') :
$offset = (($savjet_per_page * $savjet_page) /* - $savjet_per_page*/ );
//$offset = $offset < 0 ? 0 :  $offset;
$args = array(
'numberposts'     => $savjet_per_page ,     
'offset' => $offset ,
'meta_key' => 'wpcf-glasova',
'orderby'         => 'meta_value_num post_date',
'order'           => 'DESC',   
//'paged' => $savjet_page , 
'post_type'       => 'savjet',    
'post_status'     => 'publish'
/*'suppress_filters' => true*/ ); 

$savjeti = get_posts($args);

…adding post_date (or even date) to orderby clause doesn’t seem to change anything (‘date’ seems to make it worse by making the list ascend even though the order is DESC).

Related posts

1 comment

  1. Solved it with:

    function myedit_posts_orderby($orderby_statement) {
        $orderby_statement = "wp_postmeta.meta_value+0 DESC, wp_posts.post_date DESC";
        return $orderby_statement;
    }
    
    add_filter('posts_orderby', 'myedit_posts_orderby');
    WP_Query...
    

    …for some reason WP_Query only applied DESC order to date, and not to meta value.

Comments are closed.