0

I am having a strange issue with pagination of custom post types, the pagination works but will only paginate up to the maximum number of standard posts.

For example:

I have 10 'operator' posts and 5 standard 'posts'. The following code will display 1 operator post per page and display pagination with 10 pages, however once I reach page 6 I receive a 404 error (over the total amount of standard posts).

      $args=array(
        'post_type' => 'post_operators',
        'post_status' => 'publish',
        'paged' => $paged,
        'posts_per_page' => 1,
      );

If I was to add another standard post I would then be able to reach page 6 of my custom post pagination and so on...

MY CUSTOM POST LOOP

<?php


       $paged = get_query_var('paged') ? get_query_var('paged') : 1;
       $args = array('post_type' => 'post_operators', 'posts_per_page' => 2, 'paged' => $paged);
       $loop = new WP_Query( $args );
       while ( $loop->have_posts() ) : $loop->the_post(); ?>


                        <article>
                        content
                        </article>

 <?php endwhile;  ?>
 <?php bones_page_navi();  ?>
 <?php wp_reset_query(); ?>

PAGINATION FUNCTION (FROM BONES THEME)

function bones_page_navi($before = '', $after = '') {
    global $wpdb, $wp_query;
    $request = $wp_query->request;
    $posts_per_page = intval(get_query_var('posts_per_page'));
    $paged = intval(get_query_var('paged'));
    $numposts = $wp_query->found_posts;
    $max_page = $wp_query->max_num_pages;
    if ( $numposts <= $posts_per_page ) { return; }
    if(empty($paged) || $paged == 0) {
        $paged = 1;
    }
    $pages_to_show = 7;
    $pages_to_show_minus_1 = $pages_to_show-1;
    $half_page_start = floor($pages_to_show_minus_1/2);
    $half_page_end = ceil($pages_to_show_minus_1/2);
    $start_page = $paged - $half_page_start;
    if($start_page <= 0) {
        $start_page = 1;
    }
    $end_page = $paged + $half_page_end;
    if(($end_page - $start_page) != $pages_to_show_minus_1) {
        $end_page = $start_page + $pages_to_show_minus_1;
    }
    if($end_page > $max_page) {
        $start_page = $max_page - $pages_to_show_minus_1;
        $end_page = $max_page;
    }
    if($start_page <= 0) {
        $start_page = 1;
    }

    echo $before.'<div class="pagination text-center center-block"><ul class="pagination pagination-centered pagination-lg">'."";
    if ($paged > 1) {
        $first_page_text = "«";
        echo '<li class="prev"><a href="'.get_pagenum_link().'" title="First">'.$first_page_text.'</a></li>';
    }

    $prevposts = get_previous_posts_link('← Previous');
    if($prevposts) { echo '<li>' . $prevposts  . '</li>'; }
    else { echo '<li class="disabled"><a href="#">← Previous</a></li>'; }

    for($i = $start_page; $i  <= $end_page; $i++) {
        if($i == $paged) {
            echo '<li class="active"><a href="#">'.$i.'</a></li>';
        } else {
            echo '<li><a href="'.get_pagenum_link($i).'">'.$i.'</a></li>';
        }
    }
    echo '<li class="">';
    next_posts_link('Next →');
    echo '</li>';
    if ($end_page < $max_page) {
        $last_page_text = "»";
        echo '<li class="next"><a href="'.get_pagenum_link($max_page).'" title="Last">'.$last_page_text.'</a></li>';
    }
    echo '</ul></div>'.$after."";
}

Can anyone see an error in either my loop of pagination function or perhaps think of something I may have missed?

3
  • you can demand helpful comments or answer only from people you pay. Here you will get comments and answers that people think might be helpful to you. If you don't like them ignore them, don't conduct a petty argument. Feb 2, 2016 at 11:47
  • @Pieter Goosen - Many thanks, apologies for taking frustrations out on you!
    – James
    Feb 2, 2016 at 14:40
  • No problem, glad you got the issue solved. ;-) Feb 3, 2016 at 6:45

0

Browse other questions tagged or ask your own question.