0

The solution <?php query_posts('posts_per_page=9'); ?> to limit author related posts show the entire website post :(

  <?php
    $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
  ?>
  <h2>About: <?php echo $curauth->nickname; ?></h2>
  <dl>
    <dt>Website</dt>
    <dd><a href="<?php echo $curauth->user_url; ?>"><?php echo $curauth->user_url; ?></a></dd>
    <dt>Profile</dt>
    <dd>
      <?php echo $curauth->user_description; ?>
    </dd>
  </dl>

  <h2>Posts by <?php echo $curauth->nickname; ?>:</h2>

  <ul>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
      <li>
        <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>">
          <?php the_title(); ?>
        </a>,
        <?php the_time('d M Y'); ?> in
          <?php the_category('&');?>
      </li>

      <?php endwhile; else: ?>
        <p>
          <?php _e('No posts by this author.'); ?>
        </p>    
      <?php endif; ?>
  </ul>
  • 1
    Use pre_get_posts hook instead of alter the main query. – Ranuka Nov 9 '16 at 11:09
1

Never, never re-query a native archive - use the pre_get_posts hook to alter the main query:

add_action( 'pre_get_posts', function ( $wp_query ) {
    if ( $wp_query->is_main_query() && $wp_query->is_author() ) {
        $paged = max( 1, ( int ) $wp_query->get( 'paged' ) );

        if ( $paged === 1 ) {
            $wp_query->set( 'posts_per_page', 10 );
        } else {
            $wp_query->set( 'posts_per_page', 9 );
            $wp_query->set( 'offset', ( ( $paged - 1 ) * 9 ) + 1 );
        }
    }
});

Add the above to your functions.php and remove query_posts( ... ) line.

  • Is it possible limit when click on next_posts_link() ? for example first page shows 10 items and the rest listing show 9 items – Muhammed Nov 9 '16 at 11:28
  • See my updated answer. – TheDeadMedic Nov 9 '16 at 11:56
  • thanks for your help. Can I use the same method for home page ? – Muhammed Nov 9 '16 at 12:11
  • Yes, change $wp_query->is_author() to ( $wp_query->is_author() || $wp_query->is_home() ) – TheDeadMedic Nov 9 '16 at 12:12
  • Interesting 😊 I am gonna apply this 👍 – Muhammed Nov 9 '16 at 12:17
0

To create secondary listings (for example, a list of related posts at the bottom of the page, or a list of links in a sidebar widget), try making a new instance of WP_Query or use get_posts().

Why ? query_posts() is meant for altering the main loop. It does so by replacing the query used to generate the main loop content.

Infos from developer.wordpress.org query_posts()

SO in your code, you'll need to create a new query,

$related_query = get_posts(array(
                     'post_type'=> 'post',
                     'post_status'=> 'publish',
                     'post_author'=> $curauth,// must be the id
                     'posts_er_page'=> 9,  
                     )
);
if ( $related_query ) {
foreach ( $related_query as $post ) :
    setup_postdata( $post ); ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php the_content(); ?>
    <?php
    endforeach; 
    wp_reset_postdata();
}             

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.