Pagination on custom wp_query in WordPress takes to 404 error page Pagination on custom wp_query in WordPress takes to 404 error page wordpress wordpress

Pagination on custom wp_query in WordPress takes to 404 error page


Had a hard time with it too :) Was easier to search when I realized it's wrong calculated post per page number, and here is a magic trick: (to be added to functions.php)

function my_post_count_queries( $query ) {  if (!is_admin() && $query->is_main_query()){    if(is_home()){       $query->set('posts_per_page', 1);    }  }}add_action( 'pre_get_posts', 'my_post_count_queries' );


Had same problem with custom post type. I had a query on 'page-template' where the pagination came with 404. I guess the main problem here is the 'slug' of custom post type identical to 'page-template' url. For example if you have a custom post type slug 'portfolio' and a page with the same name, pagination on that page gives a 404. So I just changed 'slug' to 'archives-portfolio' and it helped


you can Change your query

$wp_query= null; $wp_query = new WP_Query();$wp_query->query("showposts=2&paged=$paged");

to

$wp_query = new WP_Query("showposts=2");

It's shown 2 Post per page and you can see and access your page 2.