Check if a specific custom field exists?

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?

1 Answer
1

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

Leave a Comment