Using WordPress ‘get_author_posts_url()’ PHP function

The get_author_posts_url() WordPress PHP function retrieves the URL to the author page for the user with the specified ID.

Usage

Here’s a simple example of how you might use the get_author_posts_url() function. Let’s say you want to create a link to the author’s page for a particular post:

$author_id = get_post_field( 'post_author', get_the_ID() );
echo '<a href="' . esc_url( get_author_posts_url( $author_id ) ) . '">' . get_the_author() . '</a>';

In this example, get_post_field('post_author', get_the_ID()) fetches the author ID of the current post. Then, get_author_posts_url($author_id) fetches the URL to the author’s page. Finally, we’re creating a hyperlink to the author’s page with the author’s name as the link text.

Parameters

  • $author_id (int) – Required. The ID of the author.
  • $author_nicename (string) – Optional. The author’s nicename (slug). Default: ”.

More information

See WordPress Developer Resources: get_author_posts_url()

This function is not deprecated as of the latest WordPress version. However, as with most get_ functions, the output of get_author_posts_url() is not escaped and requires escaping for safe usage.

Examples

Basic Usage

This example displays a link to the author’s page for the current post.

$author_id = get_post_field( 'post_author', get_the_ID() );
echo '<a href="' . esc_url( get_author_posts_url( $author_id ) ) . '">' . get_the_author() . '</a>';

Using get_the_author_meta

This example also displays a link to the author’s page for the current post, but uses get_the_author_meta('ID') to fetch the author ID.

$author_id = get_the_author_meta('ID');
echo '<a href="' . esc_url( get_author_posts_url( $author_id ) ) . '">' . get_the_author() . '</a>';

Including Author Nicename

This example includes the author’s nicename (slug) as part of the URL to the author’s page.

$author_id = get_post_field( 'post_author', get_the_ID() );
$author_nicename = get_the_author_meta('user_nicename', $author_id);
echo '<a href="' . esc_url( get_author_posts_url( $author_id, $author_nicename ) ) . '">' . get_the_author() . '</a>';

Fetching URL for Specific Author

This example fetches the URL to the author’s page for a specific author ID (in this case, 5).

echo '<a href="' . esc_url( get_author_posts_url( 5 ) ) . '">Author Name</a>';

Fetching URL for Specific Author with Nicename

This example fetches the URL to the author’s page for a specific author ID and nicename (in this case, ID 5 and nicename ‘johndoe’).

echo '<a href="' . esc_url( get_author_posts_url( 5, 'johndoe' ) ) . '">John Doe</a>';

In each of these examples, get_author_posts_url() is used to fetch the URL to the author’s page. The