10

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

1

4 Answers 4

20

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.

5
  • 1
    This problem comes up all of the time. IMHO, custom post types are not regular posts, so there should be a separate posts_per_page query variable for each post type. Anyhoo, here's an answer that shows how to fiddle with the posts_per_page value on the fly without messing with setting in the admin: wordpress.stackexchange.com/questions/30757/… Feb 4, 2012 at 0:41
  • That question by goto10 works like a charm and it's better if you still want to use the posts per page setting for other pages, I'm still curious on why this happens, this is the first time it happened to me I've been able to user posts_per_page like normal in before without it clashing with WP settings. Feb 23, 2012 at 11:50
  • 2
    Thanks a ton for this info! Been pulling my hair out messing around with rewrite rules and everything else, and it turns out this is the issue.
    – Chaser324
    Aug 27, 2012 at 19:11
  • It's 2019, and this answer is still valid! Amazing.
    – Cenlan
    Dec 17, 2019 at 13:13
  • Thanks, very helpful. Apr 19, 2022 at 20:19
9

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;
}
1
  • 1
    This actually worked for me. The other solution (WP-Admin / Settings / Reading / 1) didn't work at all. Thanks for this! (hours wasted debugging and searching)
    – TCattd
    Oct 14, 2014 at 22:03
0

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

1
  • Thanks, I did give this a go but the problem still remained. Jul 10, 2011 at 9:51
0

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; 
}
2
  • Can you explain your solution?
    – tehlivi
    May 13, 2021 at 1:32
  • The code seems to show everything, it rebuilds the query string correctly. If any page is open, but it is not mistakenly reflected in the URL, rebuild and add the missing slug to the section.
    – ktscript
    May 13, 2021 at 4:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.