Get the post Author ID outside the WP Loop

14,969

Please use function as following:

$author_id = get_post_field ('post_author', $post_id);

It will work.

Share:
14,969
Kareen Lagasca
Author by

Kareen Lagasca

I am an Accountant working in a Multinational Oil Company. I really love web design, unfortunately, I have no time to get another degree. I know Wordpress, HTML, CSS and a very little knowledge in PHP. I studied them through Google, Stackoverflow and other forums like this. Please be patient with me :)

Updated on July 20, 2022

Comments

  • Kareen Lagasca
    Kareen Lagasca almost 2 years

    What I'm trying to accomplish is the code will auto detect the Author ID <?php the_author_ID(); ?> of the blogpost. Once it has the author ID, it will do a simple task. If the author # is equal to 2, call function sBadong. Else, if author ID is equal to 3, call function sJade. if two condition was not met, call function sBen.

    here's my code, but it's not working. I dunno what wrong's with it. can you help?

    <?php
    $author_id=$post->post_author;
    if ($author_id == "2") {
        echo sBadong();
    } elseif ($author_id == "3") {
        echo sJade();
    } else {
        echo sBen();
    }
    ?>
    

    The problem with the code above is it's not reading the Author Number of the post. It always returning the function sBen(); and ignore all if and else statements.