How does offset works on pagination? (get_results)

The problem here is the LIMIT, it’ll just count the first page and not the entire query.

I had solved it by providing a secondary SQL query for counting the max pages. thanks for my friends for this tip.

here’s the complete code.

function.php

function spiciest(){
global $wpdb, $paged, $max_num_pages;

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$post_per_page = intval(get_query_var('posts_per_page')); //6
$offset = ($paged - 1)*$post_per_page;


// query normal post
$query_spicy = "
    SELECT DISTINCT * FROM $wpdb->posts
    INNER JOIN (SELECT *, SUBSTRING(name, 6) as 'post_ID',
    votes_up  AS votes_balance,
    votes_up + votes_down AS votes_total
    FROM thumbsup_items) AS thumbsup
    ON $wpdb->posts.ID = thumbsup.post_ID
    WHERE $wpdb->posts.post_status="publish"
    AND $wpdb->posts.post_type="post"
    AND $wpdb->posts.post_password = ''
    ORDER BY votes_up DESC, votes_balance DESC";


//query the posts with pagination
$spicy = $query_spicy . " LIMIT ".$offset.", ".$post_per_page."; ";

$spicy_results = $wpdb->get_results( $spicy, OBJECT);

// run query to count the result later
$total_result = $wpdb->get_results( $query_spicy, OBJECT);


$total_spicy_post = count($total_result);
$max_num_pages = ceil($total_spicy_post / $post_per_page);


return $spicy_results;
}

TEMPLATE CODES:

<?php
 $spiciest = spiciest();

 if ($spiciest):
    global $post;
    foreach ($spiciest as $post) :
        setup_postdata($post);
?>

/**** PUT TEMPLATE TAGS HERE *****/


<?php
    endforeach;
endif;

?>

and then the PAGINATION here, please note of the TOTAL in array.

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

$pagination = array(
    'base' => @add_query_arg('page','%#%'),
    'format' => '',
    'total' => $max_num_pages,
    'current' => $current,
    'prev_text' => __('PREV'),
    'next_text' => __('NEXT'),
    'end_size' => 1,
    'mid_size' => 2,
    'show_all' => false,
    'type' => 'list'
);

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' ) );

echo paginate_links( $pagination );