I've one custom post type name of "videos" now I want to query in a custom template from that CPT. Here is my custom post register code

register_post_type( 'videos',
                'labels' => array(
                        'name' => __( 'Videos' ),
                        'singular_name' => __( 'Video' ),
                        'add_new' => __( 'Add New Video' ),
                        'add_new_item' => __( 'Add New Videos' ),
                        'edit_item' => __( 'Edit Videos' ),
                        'new_item' => __( 'New Videos' ),
                        'view_item' => __( 'View Videos' ),
                        'not_found' => __( 'Sorry, we couldn\'t find the Videos you are looking for.' ),
                        'featured_image'  => __('Video Thumbnail'),
                        'set_featured_image'  => __('Set Thumbnail Image'),
                        'remove_featured_image' => __('Remove Thumbnail Image'),
                        'use_featured_image'    => __('Use Thumbnail Image'),
        'public' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => true,
        'menu_position' => 14,
        'has_archive' => false,
        'hierarchical' => false,
        'menu_icon'   => 'dashicons-video-alt2',
        'capability_type' => 'page',
        'rewrite' => array( 'slug' => 'videos' ),
        'supports' => array( 'title', 'thumbnail' )

And I've queried and tried a lot of ways. but didn't luck. here are my query codes.

        $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
                  global $paged;
                  global $wp_query;
                  $temp = $wp_query; 
                  $wp_query = null; 
                  $wp_query = new WP_Query(); 
                  while ($wp_query->have_posts()) : $wp_query->the_post(); 
<!--Single Video-->
<div class="col-xl-2 p-0">
  <div class="single-video-area clearfix">
    <div class="single-video-data">
      <div class="single-video-thumbnail">
        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('video-testimonial') ?></a>
        <div class="single-video-thumb-overlay">
          <a href="<?php the_permalink(); ?>" target="_blank"><img src="<?php echo get_theme_file_uri('images/controls_play.png'); ?>" alt=""></a>
</div><!--/ Single Video-->
<?php endwhile; ?>
<!--Pagination -->
<div class="video-template-pageination">
    <?php the_posts_pagination(array(
    'screen_reader_text'    => ' ',

</div><!--/ Pagination -->  
  $wp_query = null; 
  $wp_query = $temp;  // Reset

Now Pagination working fine and show after 2 posts. here is screenshot

enter image description here

But now the problem is when I click 2 or 3 from this pagination then show 404 error page here is another screenshot for 404 page

enter image description here

I've resaved permalink a lot of times. but still didn't luck. I've copied some query codes from my previous project, which project I did succeed about is topic (CPT with Pagination) but with this project didn't luck.

Already I've researched a lot of time. here is some article which I've followed, for example

Custom post type pagination 404 fix?

Change Posts per page count



But still, I'm not a success. Please help me with this issue.

Note: I've tried to replace "paged" to "page" from here

(get_query_var('paged')) ? get_query_var('paged') : 1;

but same result.

  • Is there a reason you're using a custom page template and not just archive-videos.php with the main loop? – Jacob Peattie Jun 25 at 8:58
  • Yeah, I'm using custom template, not main loop or archive-videos.php file. How can i use pagination with custom template? – Md Abul Bashar Jun 25 at 10:10
  • Why are you doing it that way? You're essentially making an archive, so why not just use the archive? – Jacob Peattie Jun 25 at 10:10
  • I need to show my all post list with pagination like this view l7pic.com/index.php/work is it possible to use the archive and get a unique link for all? because the client wants to use a custom link, like l7pic.com/index.php/work or l7pic.com/index.php/our-all-videos, etc. – Md Abul Bashar Jun 25 at 10:31
  • Yes. Just set has_archive to the slug that you want to use for the archive. – Jacob Peattie Jun 25 at 10:37

Your Answer

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

Browse other questions tagged or ask your own question.