Skip to main content
2 of 14
edited tags; deleted 4 characters in body; deleted 1 character in body; added 5 characters in body

Intentionally exceed max_num_pages on main query without getting 404?

I'm using a self-baked author.php template on WordPress 4.6.1 which by default makes an "internal" WP_Query (global $wp_query, or the so called main loop) that gets all posts from a specific author. But I also make another custom WP_Query (that I shall call $secondloop) that gets posts of a custom post type from said author too.

I've also set posts_per_page on both loops, and I want to paginate both.

$secondloop has its paged parameter set to the same page as the main loop by adding "paged" => (get_query_var('paged')) ? get_query_var('paged') : 1; to my argument array when initializing my new WP_Query.

THE PROBLEM:

Suppose that max_num_pages on both loops is 3.

  • If the main loop gives me 5 posts and my $secondloop gives me 2, then calling page 2 works no problemo. It shows me the remaining two posts from the main loop and none from my $secondloop (since $secondloop->have_posts()is false, so it doesn't go inside my while loop).

  • BUT if the main loop has 2 posts and $secondloop has 5, then calling page 2 results in a 404. :(

I obviously want to show the remaining 2 posts from $secondloop.

Changing anything inside the author.php template seems to be "too late", because it seems that no PHP code in there is executed any more, once WordPress detects that max_num_pages from the main loop is exceeded.

How can I "override" that behaviour?