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;
  • 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 Aug 7 '17 at 16:29
0

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.

0

Not working after adding this code to single.php

$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
}

}

I get this error

Parse error: syntax error, unexpected '}' in D:\...\...\single.php on line 215

When I remove '}' I get this error

Fatal error: Call to undefined function posts() ) in D:\...\...\single.php on line 213  

Here is the line 213

while ( $rel_posts->have-posts() ) {

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.