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',
array(
'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.
<?php
$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();
$wp_query->query('posts_per_page=2&post_type=videos'.'&paged='.$page);
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>
</div>
</div>
</div>
</div><!--/ Single Video-->
<?php endwhile; ?>
<!--Pagination -->
<div class="video-template-pageination">
<?php the_posts_pagination(array(
'screen_reader_text' => ' ',
));?>
</div><!--/ Pagination -->
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
Now Pagination working fine and show after 2 posts. here is screenshot
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
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.