Pagination in custom post type in wordpress

I am try to put pagination in my custom post in wordpress. my custom post type name is videos. it appears the pagination but when I click on the pagination page it goes to 404 page.

<?php 
$videos= new WP_Query(array(
    'post_type'=>'videos',
    'posts_per_page' => 9,

));?>


<?php if($videos->have_posts()) : ?>
    <?php while($videos->have_posts())  : $videos->the_post(); ?>
        <div class="col-xs-12 col-sm-6 col-md-4 col-lg-4">
            <div class="video">
                <?php the_post_thumbnail(); ?>
                <div class="watch">
                    <a href="<?php the_permalink(); ?>"><i class="fa fa-play"></i></a>
                </div>
            </div>
            <div class="video-exerpt">
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </div>
        </div>

    <?php endwhile; ?>
    <div class="col-xs-12 text-center">
        <?php
            $GLOBALS['wp_query'] = $videos; 

            the_posts_pagination(
                array(
                    'mid_size' => '2',
                    'prev_text' => '<i class="fa fa-hand-o-left"></i> Previous',
                    'next_text' => 'Next <i class="fa fa-hand-o-right"></i>',
                    'screen_reader_text' => ' '
                    )
            );
        ?>
    </div>
<?php else :?>
    <h3><?php _e('404 Error: Not Found', 'Bangladesh Parjatan'); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>

enter image description here

Read More

its shows the pagination bt the links are not working. Please help me.

Related posts

3 comments

  1. pass your wp_query arguments like this. You should use paged argument for pagination.

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
      $videos= new WP_Query(array(
                            'post_type'=>'videos',
                            'posts_per_page' => 9,
                            'paged' => $paged,
                        ));
    

    hopfully your pagination will work fine.

  2. Can you please replace below code?

    <?php 
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    $videos= new WP_Query(array(
        'post_type'=>'videos',
        'posts_per_page' => 9,
        'paged' => $paged,
    )); ?>
    
    <?php if($videos->have_posts()) : ?>
    <?php while($videos->have_posts())  : $videos->the_post(); ?>
    <div class="col-xs-12 col-sm-6 col-md-4 col-lg-4">
        <div class="video">
            <?php the_post_thumbnail(); ?>
            <div class="watch">
                <a href="<?php the_permalink(); ?>"><i class="fa fa-play"></i></a>
            </div>
        </div>
        <div class="video-exerpt">
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </div>
    </div>
    
    <?php endwhile; ?>            
        <?php 
        $total_pages = $videos->max_num_pages;
    
        if ($total_pages > 1){
    
            $current_page = max(1, get_query_var('paged'));
    
            echo paginate_links(array(
                'base' => get_pagenum_link(1) . '%_%',
                'format' => '/page/%#%',
                'current' => $current_page,
                'total' => $total_pages,
                'prev_text'    => __('« prev'),
                'next_text'    => __('next »'),
            ));
        }
        ?>    
    <?php else :?>
    <h3><?php _e('404 Error&#58; Not Found', 'Bangladesh Parjatan'); ?></h3>
    <?php endif; ?>
    <?php wp_reset_postdata();?>
    
  3. Try the following code:

       
    
    <?php 
    
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    $videos= new WP_Query(array(
        'post_type'=>'videos',
        'posts_per_page' => 9,
        'paged' => $paged,
    )); ?>
    
    <?php if($videos->have_posts()) : ?>
    
    <?php while($videos->have_posts())  : $videos->the_post(); ?>
    
    <div class="col-xs-12 col-sm-6 col-md-4 col-lg-4">
    
        <div class="video">
        
            <?php the_post_thumbnail(); ?>
            
            <div class="watch">
            
                <a href="<?php the_permalink(); ?>"><i class="fa fa-play"></i></a>
                
            </div>
            
        </div>
        
        <div class="video-exerpt">
        
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            
        </div>
    
    
    <?php endwhile; ?>            
        
    <?php else :?>
    
    <h3><?php _e('404 Error&#58; Not Found', 'Bangladesh Parjatan'); ?></h3>
    
    <?php endif; ?>
    
    </div>
    
    <nav>
    								<ul class="pagination">
                    
    									<?php
                      
    $big = 999999999; // need an unlikely integer
    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $the_query->max_num_pages
    ) );
                                        ?>
    								</ul>
    							</nav>
    
    <?php wp_reset_postdata();?>

Comments are closed.