Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Many people report problems with pagination of custom queries or custom post type archives: url/page/2 produces a 404 even though all the setup is correct.

Many end up figuring out that this is occurring because the posts_per_page of their custom loop is less than the maximum posts per page setting in the admin, which they fix by changing

 Settings » Reading » "Blog pages show at most: ____ posts" 

to 1.

This works in my situation, but I would much prefer to change this only for this specific custom query, rather than the whole site. Is this possible?

The relevant WP_Query is this:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wp_query = new WP_Query( array(
'post_type' => 'publication',
'posts_per_page' => 5,
'paged'=>$paged));
share|improve this question
add comment

1 Answer 1

I assume you have a custom post type: publication

in functions.php place that code:

function mk_parse_query($query) {
  if ($query->query_vars['post_type'] == 'publication')) {
        set_query_var( 'posts_per_page', 5 );
  }
}
if( !is_admin() ) {
    add_filter('parse_query','mk_parse_query');
}

Similar code works for me, but in my case I was struggling with 404 on custom taxonomies paged urls.

Hope this helps.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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