I'm trying to understand why my pagination gets 404 error on each page starts from second. Right now in Reading settings I've set Blog pages show at most to 1.

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $query = new WP_Query(
            'post_type'     => 'behold_testimonials',
            'orderby'       => 'rand',
            'paged'         => $paged

    if ($query->have_posts()):
        while ($query->have_posts()): $query->the_post();
            include( locate_template( '/elements/testimonials-thumbnail.php', false, false ) );

        <div class="l-pagination">

            // kirki settings
            $pagination_layout = get_theme_mod( 'posts_general__pagination-style', true );
            if( 'layout-1' == $pagination_layout ) {
                $pagination_layout_style = 'pagination-1';
            } elseif( 'layout-2' == $pagination_layout ) {
                $pagination_layout_style = 'pagination-2';

            <div class="c-pagination <?php echo $pagination_layout_style; ?>">

            $total_pages = $query->max_num_pages;
                echo paginate_links( array(
                    'base'          => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
                    'format'        => '?paged=%#%',
                    'total'         => $total_pages,
                    'current'       => max(1, get_query_var('paged')),
                    'prev_text'     => __( '<i class="fas fa-caret-left"></i>', 'behold-standard' ),
                    'next_text'     => __( '<i class="fas fa-caret-right"></i>', 'behold-standard' ),
                ) );





Where is my mistake?

  • 1
    If you want to modify the main query you should use pre_get_posts to modify it, don't throw it away and replace it with a brand new custom query. You'll be getting a 404 because the main query doesn't have that page, but your new query does. Is this a page template? Or is it that this is a sub-section that you're trying to give its own independent pagination? Note that random ordering is extremely heavy on the database, it's one of the heaviest slowest things you can do in a post query. It's always better to simulate randomness, e.g. choose a random date then ask for the closest post
    – Tom J Nowell
    Apr 27 '20 at 11:21
  • This is section in page template that I wanna set to show all posts from my CPT.
    – Damian P.
    Apr 27 '20 at 11:57
  • Ok... thanks for paying attention for random query. Im gonna check how to do what you've mentioned :)
    – Damian P.
    Apr 27 '20 at 11:59

The solution is that CPT should have 'has_archive' set to 'true'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.