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.

Below is the code from my author.php file. The problem is that next_posts_link and previous_posts_link only generate links based on the number of posts in the blog authored by the author in question, not the number of posts in the query ( which includes pages and events). I have used WP_Query to add the custom post type 'events' as recommended here. I find this behaviour strange because the page is displaying the results of the query but the previous/next_posts_link's are doing something completely different. Can anyone see my mistake or is this some problem with WordPress.

<?php get_header(); 
// add evemts post type to query          
$query = new WP_Query( array( 
  'post_type' => array( 'post', 'page', 'events' ) 
  ));

  if($query->have_posts()) : ?>
  <h2 class="title">Posted by : author </h2>
   <div id="main">
        <div id="content">                                  
            <?php while($query->have_posts()) : $query->the_post(); ?>        
            <div class="archive-post" id="<?php the_ID(); ?>">                  
                <div class="blog-entry">
                <h3 class="blog-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>                                   
            <div class="entry-meta">Entry meta here</div>
                    <?php the_excerpt(); ?>
                <div class="entry-utility">                                                                                                
              <?php edit_post_link( 'Edit', '<span class="edit-link">', '</span>' ); ?>
            </div>
          </div>                  
            </div> <!-- .post -->            
            <?php endwhile; endif; ?>                         


          <!-- navigation -->
            <div class="navigation clear">
                <div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
                <div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
            </div>


    </div>  <!-- end #content -->        
  <?php get_sidebar(); ?>    
  </div><!-- #main -->
<?php get_footer(); ?>

EDIT : I have just found this page detailing the problem, however after implementing the solution, while it is definitely an improvement in that the links now show, still for some reason they point me to a 404 page

share|improve this question
add comment

2 Answers

When using custom post types I got around this problem by using the following code. Of course you will need to put in the HTML for how you want it to display on your site.

<?php 

    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $wp_query->query(''); // Enter you query here

 ?>

<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

// Post body

<?php endwhile; ?>

<?php previous_posts_link('Previous page'); ?>
<?php next_posts_link('Next page'); ?>

<?php $wp_query = null; $wp_query = $temp;?>
share|improve this answer
1  
Thanks, This may work but I actually got it working see my answer. Also I was a bit reluctant to query directly in WP. I usually manage to avoid it. –  byronyasgur Mar 15 '12 at 20:37
add comment
up vote 0 down vote accepted

I got it working, first by implementing the solution I described in the edit above, and then by adding the code below ( from this question ) to functions.php. By the way I actually still had a problem at that point with a 404 on the last page which seemingly is caused by having 'posts_per_page' set to less than the value in the WordPress settings, I was happy to use the WordPress settings myself so I just left it out of the query.

    function custom_author_archive( &$query ) {
    if ($query->is_author)
        $query->set( 'post_type', array( 'post', 'page', 'events' ) );
    }
    add_action( 'pre_get_posts', 'custom_author_archive' );
share|improve this answer
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.