WordPress custom post type category pagination 404 Error

Creating a secondary query or overwriting the main query inside a page template is the quickest and easiest way I know to break pagination.

The main query, which determines which page to load runs before your template thus the results on the page and the query that loads the page become out of sync. The main query does not know about your in-template modifications to the query. It doesn’t matter that your in-template query modifies the “main” query. The changes come too late.

What you need to be doing is use a filter on pre_get_posts. Unless I am reading it wrong, all you need to do is set the post_status.

function set_post_type_for_archive_wpse_109213($qry) {
  if ($qry->is_archive()) {
    $post_type = $qry->get('post_type');
    if (empty($post_type)) {
      $post_type = array('post');
    }
    $post_type[] = 'review';
    $qry->set('post_type',  $post_type);
  }
}
add_action('pre_get_posts','set_post_type_for_archive_wpse_109213');

Unless you are the WordPress Core, and you aren’t, query_posts is never applicable. Just don’t use it.

Leave a Comment