I have peculiar requirement. The author.php template is used as a profile page for the author where he can view his own posts and posts by other authors. This template has two custom loops to achieve this. The first loop is for displaying post by the author himself, whose profile is being viewed. So if the URL reads “http://example.com/author/john”. The first loop is showing John’s posts. Pagination for the first loop works absolutely fine.
Arguments for the first loop:
$args = array(
'post_type' => 'portfolio_post',
'posts_per_page' => 9,
'paged' => $paged,
'orderby' => 'date',
'post_status' => 'publish',
'author' => $author_id
);
$custom_query = $wp_query;
$wp_query = new WP_Query( $args );
?>
Arguments for the second loop:
$authors = '2,3,5';
$args = array(
'post_type' => 'portfolio_post',
'posts_per_page' => 9,
'paged' => $paged,
'orderby' => 'date',
'post_status' => 'publish',
'author' => $authors
);
$custom_query = $wp_query;
$wp_query = new WP_Query( $args );
?>
The second loop is designed to show posts by other authors. The problem is with the pagination for the second loop. Since, I am viewing John’s author page WP_Query is by default set by WordPress to return posts only from the author John, whereas my second loop is actually looking for posts by other authors. Now, if John does not have any posts then pagination for the loop that fetches other authors post breaks, as $WP_Query->found_posts results “0”. I am using Ajax to display results from both loops and it’s working absolutely fine.
I hoping this behaviour could be modified by using something like the “pre_get_posts” hook or may be something else. I could also be wrong in using the template “author.php” for this. Maybe I should use a different template?
Please advise. Desperately need this working.
First: why abuse the
author.php
template file in this way? I would suggest that you consider creating a custom Page template for this output.Second: if you’re using multiple queries, it would probably be helpful to give each query a unique variable. (Personally, I would give each query a descriptive variable name, rather than just a unique variable name.)
Third: archive pagination gets tricky when using custom queries. There’s a “hackish” way to make it work, though:
e.g.
e.g.
e.g.
e.g.
This method will output pagination using whatever query you apply to the global
$wp_query
.