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.

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.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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:

  1. Move the default query into a temporary variable:

e.g.

<?php
global $wp_query;
$original_query = $wp_query
?>
  1. Nullify the default query:

e.g.

<?php
$wp_query = null;
?>
  1. Move your custom query into the global variable:

e.g.

<?php
$wp_query = $custom_query;
?>
  1. After you output your pagination template tags, restore the original query:

e.g.

<?php
$wp_query = $original_query;
?>

This method will output pagination using whatever query you apply to the global $wp_query.

share|improve this answer
    
Hi Chip, This works! I created a page template and made changes per your suggestion. However, there is still a problem. The reason for using author.php template was that I could fetch the author name using get_query_var('author_name'). Now with a page template I need to define a custom query_var and URL rewrite rules to handle the author name passed via the URL. The pagination fails to work with this when using the page template. Any ideas on this? Thx for the help! –  John Oct 2 '11 at 15:26
    
To add to my last comment. I tested with page template with custom query_var but without URL rewrite for prettying the URL. So now the issue appears only when URL rewrite. What could be the issue? –  John Oct 2 '11 at 16:04
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.