0

I want to return information about a blog. It could be, author name, title, date, country whatever just to test.

<?php
    $post_id = get_the_ID();
    while ($post_id != 0) 
        {
            $queried_post = get_post($post_id);
            $author_id = $queried_post->post_author;
            echo get_the_author_meta('display_name',$author_id);
            $post_id++;
        }
?>

In the following code, I'm trying to get all the author's name between post_id 20 to 25. When the code is run, it only displays:

The author is: 
The author is: 
The author is: 
The author is: 
The author is: 
The author is:

Can you tell me how i would fix it so that it would return any kind of information about the blog (in this case, the name of the author).

THE UPDATED CODE DOESN'T DISPLAY ANYTHING.

  • The author is: <?php the_author_meta( 'display_name', $author_id ); ?> - do you have something like that in your code? Or maybe echo 'The author is: ' . get_the_author_meta( 'display_name', $author_id ); ? And if you're in the standard Loop (while ( have_posts() ) ...), you wouldn't need to specify the second parameter there (the $author_id - i.e. the user ID). – Sally CJ May 9 '19 at 20:12
  • @SallyCJ how would i return $post_id using that code? $author_id = $queried_post->post_id; Did that but nothing. – – J patel May 10 '19 at 17:21
  • I'm sure you already have the answer by now, @Jpatel. :) – Sally CJ May 10 '19 at 19:15
0

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

        } 
    ?>
  • Hey @Qaisar, In the above code, yk how we r getting only post_id's from 20 to 25. 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? – J patel May 9 '19 at 18:59
  • u can check my code and tell me what I need to do. I'm editing the home.php file and i don't know if it would effect all the blog posts. – J patel May 9 '19 at 19:03
  • @Jpatel in that case you can use get_posts() and loop through posts using foreach . There are many examples that may help you. – Qaisar Feroz May 9 '19 at 19:48
  • @Jpatel please see the updated answer – Qaisar Feroz May 10 '19 at 10:29
  • how would i return $post_id using that code? $author_id = $queried_post->post_id; Did that but nothing. – J patel May 10 '19 at 16:06
0

You will want to get the author name inside the loop. Once your inside the loop you will be dealing with one post at a time, then you can access any post info you need. https://codex.wordpress.org/Class_Reference/WP_Post

**Qaisar has it right for your while loop. Inside the loop you have to get the post. $queried_post = get_post($post_id); then you can get the author...

UPDATED AGAIN

<?php

    $args = [
    'post__in' => range( 20, 25 ), //**get post in your range**
    ];
    $queried_posts= get_posts( $args ); // **now you have the post you want**

    foreach( $queried_posts as $queried_post ) {  // **loop through**
        $author_id = $queried_post->post_author;
        echo get_the_author_meta('display_name', $author_id) .'</br>';
    }
?>
  • Check updated code – J patel May 9 '19 at 16:18
  • @J.Bigham $author_id = $post->post_author; will not work properly without setup_postdata( $post );. On first line you missed ; . – Qaisar Feroz May 9 '19 at 17:31
  • 1
    good catch @Qaisar! I fixed it. Thanks – J.Bigham May 9 '19 at 17:36
  • @J.Bigham can you please help me? I've updated my code. – J patel May 9 '19 at 19:39
  • Are you still not seeing results? It looks like you removed the hard coded $post_id = 20; – J.Bigham May 9 '19 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.