Custom post type pagination 404 fix?

Been trying to get this working all day but haven't had any luck...

I have a custom post type called 'news' and an archive template (archive-news.php), here I'd like to show 2 posts with pagination, which works fine, until I try to go to the 'next page' (/news/page/2) which returns an error 404.

Any idea how to fix this / what I'm doing wrong?

I've literally spent all day trying to find a solution to this by searching Google and the WordPress forms, I found lots of solutions, none of which have worked so far.

My custom post type function: http://pastebin.com/uG1L6YNu

My rewrite rules print: http://pastebin.com/jbaDANYr

Topic post-type pagination 404-error archives custom-post-types Wordpress

Category Web


I found a solution here : http://walrusinacanoe.com/web-development/742

It is elegant and functional :

add_action( 'parse_query','changept' );
function changept() {
    if( is_category() && !is_admin() )
        set_query_var( 'post_type', array( 'post', 'your_custom_type' ) );
    return;
}

try this variant:

add_action('pre_get_posts','custom_pre_get_posts'); 
function custom_pre_get_posts( $query ) 
{  
    if( $query->is_main_query() && !$query->is_feed() && !is_admin()) {
        $query->set( 'paged', str_replace( '/', '', get_query_var( 'page' ) ) );  
    }  
} 

add_filter('request', 'custom_request');
function custom_request($query_string )
{ 
    if( isset( $query_string['page'] ) ) { 
        if( ''!=$query_string['page'] ) { 
            if( isset( $query_string['name'] ) ) { 
                unset( $query_string['name'] ); 
            } 
        } 
    } return $query_string; 
}

This is now working for me... for those of you having the same issue it turns out all the code was correct.

The problem was WordPress is setup to show 10 posts per page by default which clashed with my query (limiting it to 2 posts) to fix the issue I changed the WordPress setting (Settings / Reading in the admin dashboard) to 1.


You have probably tried this, but resetting the permalinks can solve this problem. Go to Settings->Permalinks and save the permalinks again.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.