I am using Elementor child theme. I have created a custom post type " project ". I have showed 3 project per page. and I have create a pagination. But this pagination does not work if I don't save " setting > reading > Blog pages show at most > 3 ". it's showing me " The page can't be found ". Althow I have 9 projects. But I don't want to depend on this " setting > reading > Blog pages show at most ". because next time if I need to show 10 post per page, then it will difficult for me. So, now I want to show 3 projects per page. I can not find where I did wrong. Can anyone help me to solve this, please. My next page permalink is this " .com/project/page/2/ ". but thi is not working. Only If I set the " setting > reading > Blog pages show at most > 3 " or low number then it's working. this is my code: $paged = get_query_var('paged') ? get_query_var('paged') : 1; $args = array( 'post_type' => 'project', // Custom post type name 'posts_per_page' => 3, // Retrieve all posts 'paged' => $paged, ); $query = new WP_Query($args); if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?> <div class="col-xl-4 col-md-6"> <a href="<?php the_permalink(); ?>" class="ad_project-item"> <?php the_post_thumbnail('full'); ?> <h4><?php the_title(); ?></h4> </a> </div> <?php endwhile; // Pagination $big = 999999999; // need an unlikely integer /*echo paginate_links(array( 'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))), 'format' => '?paged=%#%', 'current' => max(1, $paged), 'total' => $query->max_num_pages ));*/ $pagination = paginate_links(array( 'base' => str_replace($big, '%#%', get_pagenum_link($big)), 'format' => 'paged/%#%', 'current' => max(1, get_query_var('paged')), 'total' => $query->max_num_pages, 'prev_text' => __('« Previous'), 'next_text' => __('Next »'), 'type' => 'list', )); var_dump('Max num page :' . $query->max_num_pages); if ($pagination) { echo '<div class="pagination">' . $pagination . '</div>'; } // Restore original post data wp_reset_postdata(); else : echo '<p>No projects found.</p>'; endif; ?>
Jese Leos
August 10, 2024
Verified user
I experienced the same issue and situation. Try the points given below to fix the issue: The page slug should be different than the post-type project. Check the screenshot below. Refresh the permalink. Go to the Settings -> Permalinks and Click the save button. Check the pagination.
Jese Leos
August 10, 2024
Verified user
At least I got a solution. // Register custom query variable add_filter('query_vars', 'registering_custom_query_var'); function registering_custom_query_var($query_vars) { $query_vars[] = 'paginate'; return $query_vars; } // Add custom rewrite rules add_action('init', 'custom_rewrite_rules'); function custom_rewrite_rules(): void { add_rewrite_rule( '^project/paginate/([0-9]+)/?', 'index.php?post_type=project&paginate=$matches[1]', 'top' ); } Added 2 filter, 1 for register custom query variable. 2 for link rewrite rule. after that that query variable has to use in the query and pagination. <?php $paged = get_query_var('paginate') ? get_query_var('paginate') : 1; var_dump($paged); $args = array( 'post_type' => 'service', // Custom post type name 'posts_per_page' => 2, // Number of posts per page 'paged' => $paged, 'order' => 'ASC' ); $query = new WP_Query($args); if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?> <h4><?php the_title(); ?></h4> <?php endwhile; // Pagination $big = 999999999; // need an unlikely integer echo paginate_links(array( 'base' => str_replace($big, '%#%', esc_url(home_url('/service/paginate/%#%/'))), 'format' => '', 'current' => max(1, get_query_var('paginate')), 'total' => $query->max_num_pages, )); // Restore original post data wp_reset_postdata(); else : echo '<p>No services found.</p>'; endif; ?> Just it.