Emarts asked “How to list posts by author?”

One of wprecipes.com readers, Emarts, asked on the forum how he can list posts from a specific author. In this recipe, I’ll show you how we can do that, buy using the $wpdb object and the get_results() method.

To achieve this recipe, you first need to know the ID of each author you want to list posts.

On your WordPress dashboard, go to Users. When you put your mouse over an username, look for the adress appearing in your browser's status bar. The user ID is displayed in the url.
Find any WordPress user ID

Once you have the ID(s), paste the following code where you want to list the post from a specific author:

$numposts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_author = 1");

echo "Posts by Author 1";
<ul>
foreach ($numposts as $numpost) {
	echo "<li>".$numpost->post_title."</li>";
}
</ul>

To list posts from a second author, simply repeat the above code and change the post_author in the query.

Leave a Comment

* Name, Email, Comment are Required