WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Hi I'm try to get an if statement to work.

I want to display some HTML code if the post is created by a specific author.

This is what I'm come up with but it doesn't work.

<?php if ( get_posts ($post->ID, 'post_author', true) == '20')
  { ?>  HTML <?php } ?> 

Thank You

share|improve this question
In what context is this code to be used? Inside the loop? Outside the loop? – Chip Bennett Nov 27 '12 at 17:10

The get_posts() function retrieves posts from the database. If I understand you correctly, that's not what you want though, you simply mean to check the author of the post currently displayed.

You can simply use $post->post_author. If outside the Loop, you have to call upon the $post global first.


global $post; // only required outside the loop

if ( 20 === $post->post_author ) {
    // do stuff

will do what you want.

share|improve this answer
Thanks Johannes that worked just need to add the ' to 20 to get '20' – user24126 Nov 28 '12 at 15:54
The $post_author property of the $post object should be an integer and not a string, hence 20 should do. If you experience it differently though, then you must be right in adding the apostrophes. Alternatively, you could use == instead of === as a comparison operator, which is less strict does not check for variable type (20 == '20' returns true whereas 20 === '20' returns false). If this helped you, you could mark this answer as correct (checkmark on the left hand). – Johannes Pille Nov 28 '12 at 16:06

Assuming you're inside the loop, you can use get_the_author(), which returns the Author Display Name:

if ( 'Author Display Name' == get_the_author() ) {
    // This post is written by the
    // specified author; do something

Note that get_the_author() accepts no parameters, and as such must be used from inside the Loop.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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