Display Related Posts by the Same Author in WordPress

WPKnifer
1 min readJun 4, 2021

Do you want related posts by the same author? You can easily do it with a plugin. Everyone doesn’t love using a plugin for simple work. Even then You can do it with simple code.

<?php
global $post;
$author_ID = get_the_author_meta(‘ID’);
$args = array(
‘author’ => $author_ID,
‘post__not_in’ => array($post->ID),
‘posts_per_page’ => 3,
‘ignore_sticky_posts’ => 1,
‘post_status’ => ‘publish’,
);
$custom_query = new wp_query( $args );
if($custom_query->have_posts()):
?>
<div>
<h3><?php echo esc_attr__(‘Related Posts’, ‘Text Domain’); ?></h3>
<ul style=”display: flex; width: 100%; flex-wrap: wrap; list-style: none;”>
<?php while ($custom_query->have_posts()) : $custom_query->the_post(); ?>
<li style=”flex: 0 0 33.333333%; max-width: 33.333333%; padding-right: 20px;”>
<?php if(has_post_thumbnail()): ?>
<a href=”<?php the_permalink(); ?>” title=”<?php the_title() ?>”>
<?php the_post_thumbnail(‘post-thumbnail’); ?>
</a>
<?php endif; ?>
<h4><a href=”<?php the_permalink(); ?>” title=”<?php the_title() ?>”><?php the_title(); ?></a></h4>
<p>Posted by: <?php echo get_the_author_meta(‘display_name’); ?> Posted on: <?php the_date(); ?></p>
<p><?php the_excerpt(); ?></p>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php
endif;
wp_reset_query();
?>

If you want to know you can visit. How to Display Related Posts in WordPress Without Plugins

--

--