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

Is it possible to check if a custom field exists for a given post within the loop? For example, I'm using functions like get_post_meta($post->ID, 'Company', true); preceded by a <h4> tag, but I'd like to check to make sure that there is a value for the custom field "Company" before writing the <h4>. Is this possible?

share|improve this question
up vote 3 down vote accepted

The WP get_ methods are here to retrieve data, not to display them. You can easily check the var - containing the data - before adding your head tags:

$my_post_meta = get_post_meta($post->ID, 'Company', true);
if ( ! empty ( $my_post_meta ) )
    echo '<h4>'.$my_post_meta.'</h4>';
share|improve this answer
    
This worked perfectly, thanks! – daysrunaway Jul 10 '11 at 4:15

Your Answer

 
discard

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.