0

Is it possible to get this function paged? I've tried to add previous and next links but i'ts not working...

function get_related_author_posts() {
            global $authordata, $post;

            $authors_posts = get_posts( array( 'author' => $authordata->ID, 'post_type' => 'kenniscentrum', 'post__not_in' => array( $post->ID ), 'posts_per_page' => 5 ) );

            $output = '<ul>';
            foreach ( $authors_posts as $authors_post ) {
                $output .= '<li><a href="' . get_permalink( $authors_post->ID ) . '">' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</a>' . apply_filters( 'the_excerpt', $authors_post->post_excerpt, $authors_post->ID ) . '</li>';
            }
            $output .= '</ul>';
      // bof previous and next links adde by me not working
            $output .= '<div class="nav-previous"> '.next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts' ) ) .'</div>';
            $output .= '<div class="nav-next"> '.previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>' ) ) .'</div>';
     // eof previous and next links adde by me not working
            return $output;

        }

        /*add this hook where you need the related posts*/
        if(get_related_author_posts()) :
            echo get_related_author_posts();
        else :    
            echo 'No articles...';
        endif;  
1

As I already stated in a comment to your answer, you should never make use of query_posts

Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).

When you need paginated queries, WP_Query is the way to go as it returns all the necessary info for the correct calculations for pagination to work correctly. The other advantage here with using WP_Query is that results are cached making them a bit faster as well

You have to do two things here:

  • Set the paged parameter in your query arguments

  • Set the $max_pages parameter in next_posts_link()

You can try something like this: (CAVEAT: This is untested)

function get_related_author_posts() {

    global $authordata, $post;

    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    $args = array( 
        'author'            => $authordata->ID, 
        'post_type'         => 'kenniscentrum', 
        'post__not_in'      => array( $post->ID ), 
        'posts_per_page'    => 5,
        'paged'             => $paged
    );
    $authors_posts = new WP_Query( $args );

    $output = '';

    if( $authors_posts->have_posts() ) {

        $output = '<ul>';

        while( $authors_posts->have_posts() ) {
            $authors_posts->the_post();

            $output .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a>' . get_the_excerpt() . '</li>';

        }

        $output .= '</ul>';

        $output .= '<div class="nav-previous"> '. get_next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts' ), $authors_posts->max_num_pages) .'</div>';
        $output .= '<div class="nav-next"> '. get_previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>' ) ) .'</div>';


        wp_reset_postdata();
    }

    return $output;

}
  • Hi, this paginations links stoped work when I've puted my site on a live server. This code has been successfully tested on my localhost but when I pass the site to live server, the pagination crashes with wordpress permalinks set (/% postname% /) and only works with the default permalinks (? p = 123). Why is this happening? – Alex Dec 19 '14 at 20:25
  • Strange, I've tested this on my local install with pretty permalinks, works perfectly. Have you flushed your permalinks again. Flush your permalinks and reload your home page. I can't really see any other reason why this should fail. By crash, do you mean 404 error. I'm off to bed now, if the problem persist, post a new question, as this problem is not caused by my code but something else. – Pieter Goosen Dec 19 '14 at 20:41
  • I also tested this on my local machine and it's working there, but I've posted the comment because I was thinking that if you have more experience than I in programming, maybe, you could have a glimpse on why this could possible happen. The problem is that it doesn't let me go to the second or third or any other pages (if they exist) but it doesn't show me any 404 page... – Alex Dec 20 '14 at 0:58
  • Are you trying to use this on a static front page? – Pieter Goosen Dec 20 '14 at 5:17
  • No. This is a listing page (single-listing.php) and I have the page like this: first it shows (by order) the single listing content, the comments, your code (to list all posts by the author of that listing), and after that are all listings related to the current one. Hope that helps... – Alex Dec 20 '14 at 13:50
0

In your code after global $authordata, $post; add

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

and change your code from

$authors_posts = get_posts( array( 'author' => $authordata->ID, 'post_type' => 'kenniscentrum', 'post__not_in' => array( $post->ID ), 'posts_per_page' => 5 ) );

to

$authors_posts = get_posts( array( 
    'author' => $authordata->ID, 
    'post_type' => 'kenniscentrum', 
    'post__not_in' => array( $post->ID ), 
    'paged' => $paged,
    'posts_per_page' => 5 ) 
);

Your function should work properly with the paged now.

  • I already tried this before but your code doesn't works also. This is driving me nuts.... – Alex Dec 16 '14 at 14:26
0

Instead of a function to place on functions.php, I find a workaround that can be placed on a template file:

<?php  
global $authordata, $post;
 query_posts( array( 
'author' => $authordata->ID, 
'post_type' => 'post', 
'post__not_in' => array( $post->ID ), 
'paged'         => $paged,
'posts_per_page' => 5 ));
if (have_posts()) : while (have_posts()) : the_post(); ?>

<p><a href="<?php the_permalink(); ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>

                    <?php endwhile; ?>
              <div id="nav-below" class="navigation">
              <?php wp_pagenavi(); ?>
              </div><!-- #nav-below -->
              <?php endif; wp_reset_query(); ?> 
  • You must never use query_posts. For paginated custom queries, make use of WP_Query – Pieter Goosen Dec 17 '14 at 4:36
  • @PieterGoosen How should I implement that? I took several hours making this work but I'm no expert... Thanks in advanced! – Alex Dec 17 '14 at 14:10

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.