Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've created a very complex custom post type system for a football website and am using custom meta boxes to store various match stats. To decide whether or not I should display a stat I check whether the data has been entered in the back end like this:

if (get_post_meta($post->ID, 'stat_red_h', true) || get_post_meta($post->ID, 'stat_red_a', true)){
    echo 'somestuff';
}

Where I echo the information within the if statement. However, in many cases the value for a stat is "0" and if "0" is entered for both the home and away stat then the row doesn't show, presumably because it counts it as empty.

How can I tell WordPress that entering "0" as a value does not count it as empty, but is a valid value? Perhaps some sort of:

if ($stat_red_h=='0') {
    $stat_red_h='REAL!!!';
}

In the functions file when saving the data? Perhaps you guys can help, this is the first time I've gone this deep into WordPress so I'm learning all the time.

Thanks, Mike

share|improve this question
    
== compares values; === compares values and types. So if you're storing your values as strings (I think that's the default for update_post_meta()), you should be able to do something like if ( $stat_red_h === '0' ) { do_something; }. –  Pat J Aug 7 '13 at 13:46

1 Answer 1

up vote 1 down vote accepted

When using the == comparison, it compares the values at the same type. So 1 is equal to "1", and true. Likewise, 0 is equal to false.

Taking a deeper look into the return values of get_post_meta, we can see that using the $single=true 3rd argument returns a blank string ('') when no value is found, not false. So you should be checking to make sure THAT isn't the case.

$post_meta_h = get_post_meta( $post->ID, 'stat_red_h', true );
$post_meta_a = get_post_meta( $post->ID, 'stat_red_a', true );
if( $post_meta_h !== '' || $post_meta_a !== '' ) {
    // do something
}

Let me know if this doesn't work for you.

share|improve this answer
    
Thank you, that was exactly what I needed! As I said, I'm still learning so I appreciate that help (and from Pat J above). Thanks to both. –  MikeyOh Aug 8 '13 at 8:14

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.