0

I want to list 10 last posts of author in author.php template. I used this code:

<?php while (have_posts()) : the_post(); ?>
    <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile;?>

But I can see only the last post of current author. Any help?

  • Before that while, there's some QUERY... that's the important part. Please, edit your Question and add this information to it. – brasofilo Nov 1 '12 at 2:32
-1

The easiest way would be to simply add:

global $query_string;
query_posts( $query_string . '&posts_per_page=-1' );

just before your code so you get :

<?php
global $query_string;
query_posts( $query_string . '&posts_per_page=-1' );
while (have_posts()) : the_post(); ?>
     <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile;?>
  • 3
    =o query_posts ! – Tom J Nowell Nov 1 '12 at 12:09
  • @TomJNowell I know and that's way i started my answer with the easiest way ! The preferred way is hooking into pre_get_posts – Bainternet Nov 1 '12 at 13:02
  • Fair dos, your answer does work afterall =] – Tom J Nowell Nov 1 '12 at 23:46
3

Rather than clutter your author template, invoke a second query, and use query_posts ( avoid query_posts ), instead use this filter on pre_get_posts instead:

function wpse_show_all_authors($query){
    // if it's an author query
    if($query->is_author()){
        // put all the posts on page 1
        $query->set('posts_per_page',-1);
    }
    return $query;
}
add_filter('pre_get_posts','wpse_show_all_authors');

Put it in functions.php and it will work its magic on any and all author templates you have, without needing to modify author.php or execute 2 DB queries instead of 1.

  • Side note: this will add a check on every page load. not like the query_post method which will only effect author archives. – Bainternet Nov 2 '12 at 19:30
  • I consider an if statement opcode to be orders of magnitude faster than a second DB query and thus worth the cost of a handful of instructions for a simple value comparison, a cost that can be eliminated trivially in even the most elegant of codebases, and that's done by WP Core itself many, many times already. – Tom J Nowell Nov 3 '12 at 1:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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