0

I am trying to use the following code to check for my custom field for a value of ZERO to then display my div

<?php 
$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?

4
  • Are you sure mp_product_price is set? Try var_dump($mp_price_sort);
    – vancoder
    Apr 8, 2013 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
    – 730wavy
    Apr 8, 2013 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, 2013 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.
    – 730wavy
    Apr 8, 2013 at 21:17

2 Answers 2

2

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.

1
  • Oh Ok, THANKS! I've used your suggestion and change a few things and now it seems to be working.
    – 730wavy
    Apr 8, 2013 at 21:46
0

This is the code I used -

<?php 
$mp_price_sort = get_post_meta($post->ID, 'mp_price_sort', true);
if ($mp_price_sort == 0) { ?>
    <div>TESTING</div>
<?php } ?>
1
  • 3
    This answer is redundant. @vancoder 's post contains the necessary information and is the correct solution.
    – s_ha_dum
    Apr 8, 2013 at 22:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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