WebAppsGuides

A guide to tech breakthroughs

How to get author posts in wordpress

ThemeFunctions

webapps Lab

May 29, 2021

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):

setup_postdata($postAuthor);?>

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

<?php endforeach;?>

<?php wp_reset_postdata();?>

<?php endif;?>

</div>

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.

get_author_posts_url(get_the_author_meta('ID'));

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();?>

</a>

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

 

 

 

Comments

Your email will not be published, Red mark sections* must be filled to comment

Related Posts

No Featured Image

How to get user profile image URL in WordPress

In this post, you will find a step-by-step process that will retrieve the user profile image URL in WordPress. The common way to get the image uses the get_avatar function. […]

No Featured Image

How to get the author role in WordPress

In WordPress, there are six basics user capabilities and roles. When the blog has many authors(users) it becomes a challenge to reveal the roles of a user. Although, it is […]

No Featured Image

Get author nickname in wordpress

WordPress allows displaying the author’s nickname instead of the real name. The actual and non-deprecated WordPress function for display is get_the_author_meta. To output the nickname, the field value of nickname […]

No Featured Image

Get Author image in WordPress without plugin

You can display the author’s images in the writer biography box which exposes the author’s social media profiles. In WordPress, there is a number of WordPress plugins that can show […]

WebAppsGuides

A guide to tech breakthroughs

E-mail : simunzuriblog@gmail.com

Company