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

I need opposite of this:

<?php if ( get_post_meta($post->ID, 'price_list_category1', true) )  : ?>style="display:none;"<?php endif; ?>

In other words I want style="display:none;" only when meta data doesn't exist.

I thought it would be straightforward like if ( get_post_meta($post->ID, 'price_list_category1', true but this true/false turns out to be a completely different stuff.

any ideas?

Thank you.

share|improve this question
up vote 1 down vote accepted

You could use the empty function inside your if as such :

<?php if( empty( get_post_meta( $post->ID, 'price_list_category1', true ) ) ) : ?>style="display:none;"<?php endif; ?>

The above returns an error, you should assign the return value to a variable. See my edit below.


empty might not be the best option depending on the values you store in the meta. Values like false, 0 etc... will be considered empty.

Check the PHP manual for the full list of values that are considered empty.


You can try assigning the meta to a variable, and using that in the if statement.

      $price_list = get_post_meta( $post->ID, 'price_list_category1', true );

And then...

 if( empty( $price_list) ) : ?>style="display:none"<?php endif; ?>
share|improve this answer
I tried this, it gives Fatal error: Can't use function return value in write context – Sandro Dzneladze Jun 26 '12 at 19:46
You could assign the meta return value to a variable and use that variable inside the if. See my edit. – Shane Jun 26 '12 at 19:53
it appears empty has limitations, and need plain variable to do its magic. one of those php limitations that make no sense. – Sandro Dzneladze Jun 27 '12 at 9:04
Some functions like empty() aren't functions in the normal sense. They're implemented as language features/keywords that evaluate a variable directly instead of passing a value to a function. – Dave Ross Mar 1 '15 at 6:18

I found this via searching for a solution myself, but it dawned on me the answer is very simple. You simply need to check if the value is empty, if it is then echo nothing - if it has content, then display the content - the code i used is below and can be tailored accordingly to anyone who needs to use it.

<?php $meta = get_post_meta( get_the_ID(), 'page-sub-title', true );
    if ($meta == '') {
        echo '&nbsp;';
    } else {
        echo '<h2>' . $meta . '</h2>';
share|improve this answer
works but why get_the_ID() instead of good ol $post->ID – gurung Apr 8 '14 at 16:01

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.