0

How can I display related post "by category" "by author"? I use this code but it just displays related posts by newest posts of author, not related posts by category of author. I've done some research on the internet but cannot find an answer.

function get_related_author_posts() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID,'post__not_in' => array( $post->ID ), 'posts_per_page' => 3 ) );
$output = '<div class="rp-users">';
foreach ( $authors_posts as $authors_post ) {
    $output .= '<div class="rp-user-entry">';
    $output .= get_the_post_thumbnail($authors_post->ID, 'related-post-size');
    $output .= '<a href="' . get_permalink( $authors_post->ID ) . '"class="rp-title">' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</a>';
    $output .= '</div>';
}
$output .= '</div>';
return $output;
1
  • Can you explain what do you mean "by category" "by author"? Do you want to get related posts first by category, and then by author? Or author's have categories?
    – gdaniel
    Commented Aug 7, 2017 at 16:29

1 Answer 1

1

Sounds like you want to get the related posts from the same category as the current post that is written by the same author. In order to do this you will want to create a custom loop using the WP_Query Class which will look something like this:

$author_id = get_the_author();
$category_id = get_the_category();

$args = array(
    'author'         => $author_id,
    'category'       => $category_id[0],
    'posts_per_page' => 5,
);

$rel_posts = new WP_Query( $args );

if( $rel_posts->have_posts() ) { 
    echo '<div class="related-posts">' . PHP_EOL;
    echo '<ul>' . PHP_EOL;
    while ( $rel_posts->have-posts() ) {
        echo '<li class="related-post"><a href="' . esc_url( get_the_permalink() ) . '>' . esc_html( get_the_title() ) . '</a></li>' . PHP_EOL;
    }
    echo '</ul>';

    // Reset Post Data
    wp_reset_postdata();

    } else {
        // No Posts Found
    }
}

What this code does is it grabs the author ID from the current post as well as the array of categories the post has (Note: get_the_category() will always return an array even if only one is assigned. It will return the ID, Name, and Slug).

We then feed it into the $args array using $category_id[0] to grab the ID of the first category in the array. For demonstration purposes, I also tell the query to only grab 5 posts.

It will then open up an unordered list and loop through the posts WP_Query found and create a list item for each one containing a link. Once it's done, we close the unordered list and reset the post data back to the original one.

Note: You can ommit the . PHP_EOL part of the echo statements. That just adds a line break at the end which I like to do for diagnostic purposes so the resulting HTML code is easier to read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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