I'm having trouble with get_next_posts_link() on a custom page template; it returns null, despite the fact that I've got many other posts available. I've seen many solutions to this problem that suggest putting the paged parameter into query_posts(), but I've got to use a custom query for this so that isn't an option. I'm not sure how (or if) I can pass the paged parameter into my custom query to make get_next_posts_link() work. Does anyone know how I can solve this, and get_next_posts_link() to work properly with my query?

Just as a note, if I manually visit any page other than the first, the get_previous_posts_link() function works fine. But the get_next_posts_link() function never works, regardless of which page I'm on. Below is the code I'm using:

My Query

function upcoming_query()   {

global $wpdb;
$totalposts = $wpdb->get_results($wpdb->prepare("
    SELECT * 
    FROM $wpdb->posts
    WHERE post_type = 'post' 
    AND post_status = 'publish'
    ORDER BY score DESC

"), OBJECT);

$ppp = intval(get_query_var('posts_per_page'));

    $wp_query->found_posts = count($totalposts);
    $wp_query->max_num_pages = ceil($wp_query->found_posts / $ppp);     
    $on_page = intval(get_query_var('paged'));  

    if($on_page == 0) $on_page = (int) 1;       

    $offset = ($on_page-1) * $ppp;

$pageposts = $wpdb->get_results($wpdb->prepare("
    SELECT * 
    FROM $wpdb->posts
    WHERE post_type = 'post' 
    AND post_status = 'publish'
    ORDER BY score DESC
    LIMIT %d OFFSET %d
", $ppp, $offset), OBJECT);

return (array) $pageposts;
}

My Pagination

function pagination()   {

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>

<div id="pagination" class="left">

    <?php   $previous_posts = get_previous_posts_link(); 
            if($previous_posts)  {
                echo $previous_posts;
            } else {
                echo "<div class='pagination_disabled left'>&laquo; Previous Page</div>";   
            } 
    ?>

    <form id="paged" named="paged" action="" method="post" class="left">
        <input type="text" name="paged_value" class="txt" value="<?php echo $paged; ?>" />
    </form>

    <?php   $next_posts = get_next_posts_link(); 
            if($next_posts)  {
                echo $next_posts;
            } else {
                echo "<div class='pagination_disabled right'>Next Page &raquo;</div>";  
            } 
    ?>

<div class="clear"></div>
</div>

<?php }

I appreciate any help you may be able to provide! Thanks!

link|improve this question

feedback

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
or
required, but never shown

Browse other questions tagged or ask your own question.