[Solved] WP_query not loading posts with pagination if posts_per_page number is larger than posts that are published

liquidRock Asks: WP_query not loading posts with pagination if posts_per_page number is larger than posts that are published
For some reason WP_query will return no posts if I set the posts_per_page to a number larger than the number of posts that are currently published of that custom post type. One would assume that posts_per_page would be a maximum. It may be a problem with the pagination code as I copied that from somewhere online (possibly this website).

If I set posts_per_page to a number larger than posts that exist (like 20 or 30), no posts load at all and instead the else text is echoed. If I set the posts_per_page “5”, then five posts load fine and everything works as more than 5 are already published in the test environment.

I need up to 20 posts to load per page regardless of how many are currently published. Within a short time of the site being live, there will be more than 20 published posts at which point the pagination should show.

Here are the args and beginning of WP_query:

Code:
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$tpr_args = array( 'post_type'      =>  'works',
    'meta_query' => array(
        'relation'      =>  'AND',
        array(
            'key'       =>  'wrks_verified',
            'value'     =>  1,
        ),
        array(
            'key'       =>  'wrks_approved',
            'value'     =>  1,
        ),
    ),
    'orderby'           =>  'date', 
    'order'             =>  'DESC', 
    'posts_per_page'    =>  20,
    'post_status'       =>  'publish',
    'paged'             =>  $paged,
);
$tpr_query = new WP_Query( $tpr_args );

if ($tpr_query->have_posts()) : while ($tpr_query->have_posts()) : $tpr_query->the_post();
// variables, get_post_meta and HTML code goes here and works fine when they load

And here is the pagination code and the end of WP_query:

Code:
endwhile;

if( $tpr_query->max_num_pages <= 1 )
        return;

    $max   = intval( $tpr_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 '<h4 class="v-nav-header">Load Older Videos</h4><div class="blog-page-navigation"><ul>' . "n";

    /** Previous Post Link */
    if ( get_previous_posts_link() ) {
        printf( '<li>%s</li>' . "n", get_previous_posts_link('<< Prev Page') );
    } else {
        echo '<li class="pag-dead-link"><< Prev Page</li>';
    }
    /** Link to first page, plus ellipses if necessary */
    if ( ! in_array( 1, $links ) ) {
        $class = 1 == $paged ? ' class="blog-page-active"' : '';

        printf( '<li%s><a href="%s">%s</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="blog-page-active"' : '';
        printf( '<li%s><a href="%s">%s</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="blog-page-active"' : '';
        printf( '<li%s><a href="%s">%s</a></li>' . "n", $class, esc_url( get_pagenum_link( $max ) ), $max );
    }

    /** Next Post Link */
    $default_posts_per_page = get_option( 'posts_per_page' );
    
    if ( get_next_posts_link(null,$default_posts_per_page) )
        printf( '<li>%s</li>' . "n", get_next_posts_link('Next Page >>', $max) );

    echo '</ul></div>' . "n";
else: echo '<p>I didn't find squat.<p>';
wp_reset_postdata();
endif;
?>

I also am trying to create an if statement so that the “Next Page” link will show up greyed out if one is on the last page of posts. This worked with the previous link, but not next link. Note: I added “null,$default_posts_per_page” because it didn’t load the next link at all without it. I tried this following code, but it did not show up:

Code:
/** Next Post Link */
$default_posts_per_page = get_option( 'posts_per_page' );
    
if ( get_next_posts_link(null,$default_posts_per_page) ){
    printf( '<li>%s</li>' . "n", get_next_posts_link('Next Page >>', $max) );
} else {
    echo '<li class="pag-dead-link">Next Page >></li>';
}

I’ve never used PHP like this before, so any advice you can offer to get this working will be appreciated. Thanks.

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.