Create Wordpress Theme From Scratch! Build Your Own

How to get author posts in wordpress

This is a short and simplified instruction to get a specific author’s posts in WordPress.

If the website has at least one author, each author’s posts can be shown on a single page.

It does simplify the author’s fans to see the posts of their favorite blog writer.

In a simple way, to get posts from an author requires the specify the author name or his ID.

That will lead to creating an array that contains the author id, number of posts per page, and others.

And the function get_posts takes the array to return the array of the posts objects.

Author Posts Code Snippet

The snippet lists all the posts of a current author.

Write the snippet in the index.php

$currentAuthor = wp_get_current_user();

$authorArgs = array (

'author' => $currentAuthor->ID,

'orderby => 'post_date',

'posts_per_page' => 5


$authorPosts = get_posts($authorArgs);

Display five posts on the page selected widget.

<div class = "authorPosts'>

<?php if($authorPosts):?>

<?php foreach($authorPosts as $postAuthor):


<li><a href = "<?php the_permalink();?>"><?php the_title();?></a></li>

<?php endforeach;?>

<?php wp_reset_postdata();?>

<?php endif;?>


Codes Explanation

First thing before you should create the object of the current author.

A function wp_get_current_user creates the object that can access the ID of the post writer.

Then, there should be an array that holds various elements including the ID.

The other elements an array can include is orderby for sorting posts and the number of posts to show.

The array object is given by the name $authorArgs.

The $authorArgs becomes a parameter of get_posts function.

get_posts returns the posts objects with the reference to the $authorArgs object.

The function setup_postdata helps to expose the $post global variable.

This gives the page ability to access blog posts which include post title, post link and others.

At the end of the loop the data is reset by wp_reset_postdata.

Author Posts Code Snippet – Method 2

This method is written inside the loop.

It shows the author posts when a visitor clicks the author name link.

Step 1: use get_author_posts_url to fetch the author link.

A function needs the author ID to retrieve the URL.

Therefore, you are required to employ get_the_author_meta  to get the author URL.

This is a complete code line.


Step 2. integrate the URL to anchor tag href property.

<a href = "<?php get_author_posts_url (get_the_author_meta('ID')?>">

<?php the_author();?>


When you create a WordPress theme include the author name section that contains the author link.