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 am trying to use the following code to check for my custom field for a value of ZERO to then display my div

$mp_price_sort = get_post_meta('mp_product_price');
if ($mp_price_sort  == '0') { ?>
    <div><?php echo do_shortcode('[shortcodes]'.$mp_buy_button.'[/shortcodes]'); ?></div>
<?php } ?> 

However it is interperting as a value of anything, thus displaying the div, but I need it to check for the number 0. How can I fix this?

share|improve this question
Are you sure mp_product_price is set? Try var_dump($mp_price_sort); – vancoder Apr 8 '13 at 21:06
How do I use var_dump? Im not familiar. I used the_meta to display values and mp_price_sort is 0 for free products and for other products it is whatever the price is. So I am trying to check for the free products which has a value of 0 – mygm26 Apr 8 '13 at 21:10
I get what you are doing, I'm try to establish if $mp_price_sort actually has any value at all. $mp_price_sort = get_post_meta('mp_product_price'); var_dump($mp_price_sort); – vancoder Apr 8 '13 at 21:12
Ok I used your suggestion and this is what it gives me - bool(false) - which is weird cause it was on a page that has a price of 25. – mygm26 Apr 8 '13 at 21:17
up vote 1 down vote accepted

You are using get_post_meta incorrectly, so it is returning FALSE every time. As PHP equates '0' and FALSE, your div will always be displayed.

The correct syntax for get_post_meta is:

<?php $meta_values = get_post_meta($post_id, $key, $single); ?>

where $post_id is the only required argument.

share|improve this answer
Oh Ok, THANKS! I've used your suggestion and change a few things and now it seems to be working. – mygm26 Apr 8 '13 at 21:46
You're welcome. – vancoder Apr 8 '13 at 21:47

This is the code I used -

$mp_price_sort = get_post_meta($post->ID, 'mp_price_sort', true);
if ($mp_price_sort == 0) { ?>
<?php } ?>
share|improve this answer
This answer is redundant. @vancoder 's post contains the necessary information and is the correct solution. – s_ha_dum Apr 8 '13 at 22:08

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.