Search code examples
wordpresspaginationcustom-post-type

Custom Post Type Pagination sends me to home page


For years I use a custom pagination script that I include to all my websites and it paginates flawlessly through posts. For the first time ever I created custom post types. My same script will not paginate through pages... Also all pages links send me to home. It displays exactly the correct pages number based on posts per page. It just won't work. I have used all scripts I could find on net.

Is there anything I am missing?

Has archive is true by the way. Below code example is from archive-drivers.php

I also used both custom CPT creation with script in functions and also used a plugin. Just to check if something was wrong.

    <?php
    $args = array(
    'posts_per_page'   => 1,
    'paged'            => $paged,
    'orderby'          => 'post_date',
    'order'            => 'DESC',
    'post_type'        => 'Drivers',
    'post_status'      => 'publish',
    'suppress_filters' => true
    );


    // get the results
    $wp_query = new WP_Query( $args );
    ?>

    <?php if ($wp_query->have_posts()): // the Loop ?>
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>



    (...my code to display the custom post type posts...)


    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>
    <!-- end of Code for the Loop -->


    <?php endif; ?>




    <!-- Code for PAGINATION -->
    <?php
    if (function_exists("custom_pagination"))
    {
    custom_pagination();
    }
    ?>
    <!-- End of Code for PAGINATION -->

    <?php wp_reset_postdata(); ?>
    <!-- end of Code for the Loop -->

and

    // Bootstrap Custom Pagination function in Functions.php
    function custom_pagination($pages = '', $range = 4)
    {  
    $showitems = ($range * 2) + 1;  

    global $paged;
    if(empty($paged)) $paged = 1;

    if($pages == '')
    {
    global $wp_query; 
    $pages = $wp_query->max_num_pages;
    if(!$pages)
    {
    $pages = 1;
    }
    }   

    if(1 != $pages)
    {
    echo '<div class="as-pagination" style="text-align:center; align:center;">'; 
    echo '<div> Page ' .$paged . ' of ' .$pages.'</div>'; 
    echo '<ul>';


    // If we want to always see th First Page link
    if($paged > 1) echo '<li><a href="' .get_pagenum_link(1). '" title="First Page" ><i class="far fa-arrow-left"></i></a></li>';


    for ($i=1; $i <= $pages; $i++)
    {
    if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
    {
    echo ($paged == $i)? "<li class=\"active\"><span>".$i."</span>
    </li>":"<li><a href='".get_pagenum_link($i)."'>".$i."</a></li>";
    }
    }


    //if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo '<li><a href="' .get_pagenum_link($pages). '" title="??">&raquo;</a></li>';

    if($paged < $pages) echo '<li><a href="' .get_pagenum_link($pages). '" title="Last Page"><i class="far fa-arrow-right"></i></a></li>';
    echo "</ul>";
    echo "</div>";
    }
    }

Solution

  • And the answer came from a FB friend and has to do with pre_get_posts. Only that way you can tell wp how many posts per page you want for your custom post type. The 'posts_per_page' in args won't work.

            function my_cptui_change_posts_per_page( $query ) {
            if ( is_admin() || ! $query->is_main_query() ) {
               return;
            }
    
            if ( is_post_type_archive( 'drivers' ) ) {
               $query->set( 'posts_per_page', 1 );
            }
        }
        add_filter( ' ', 'my_cptui_change_posts_per_page' );