You need to re-arrange your code.
<?php
$post_id = 20;
while($post_id <= 25) {
$queried_post = get_post($post_id);
$author_id = $queried_post->post_author;
echo get_the_author_meta('display_name', $author_id);
$post_id++;
}
?>
Within while
loop above you have $queried_post
which is an object of class WP_Post
. Member Variables of WP_Post can be used to display data about each post.
I hope this may help.
UPDATE
what if i want to the loop to apply to all of the post that there are on the site. What would be the changes i would need to make in order for me to apply to all the posts?
In that case you may code like that using get_posts()
which returns an array of objects
<?php
$arg = array( 'numberposts' => -1 ); // get all posts
$queried_posts = get_posts( $arg);
// Now loop through $queried_posts
foreach( $queried_posts as $queried_post ) {
$author_id = $queried_post->post_author;
echo get_the_author_meta('display_name', $author_id);
}
?>