1

I'm trying to do a simple if function on my code but for some reason it just isn't working correctly. I've gone over it several times to see if there is anything I'm missing, but no luck. I'm trying to say my value is 0 then echo nothing if not else the_ratings. Very simple...

<?php if( get_post_meta( $post_id, 'ratings_users', true ) === '0' ) {
}else{
the_ratings();
} ?>
<?php if(!get_post_meta( $post_id, 'ratings_users', true ) !='0' ) {
}else{
the_ratings();
} ?>
<?php if(get_post_meta( $post_id, 'ratings_users', true ) =='0' ) {
}else{
the_ratings();
} ?>

Edit: at this point I've tried 3 different ways to get this stupid thing to output nothing if the value in the custom field is 0 and still it doesn't work correctly.

2
  • Can you try to explain, using prose, what you're trying to do? It seems clear to me, based on the answers given, that we're uncertain whether you're doing a true/false comparison, a $value = '0' string comparison, or an isset() comparison. My assumption is that you're actually trying to determine if particular post meta is set before outputting it, but it is difficult to tell for sure. Aug 16, 2011 at 14:10
  • yes im trying to tell it if the post meta is the number "0" output nothing and its just not working correctly...
    – user7924
    Aug 16, 2011 at 15:30

6 Answers 6

2

The reason you have a problem is 0 is also considered equal to false, when the get_post_meta call returns false, it's also the same as being equal to 0.

if( !get_post_meta( $post_id, 'some-non-existant-field', true ) == 0 )

Would be the same as ...

if( get_post_meta( $post_id, 'some-existing-field', true ) == 0 )

..the only difference is in one case the field doesn't exist, and in one it does(and has a zero value), both will be true though.

Additionally, 0 is not the same as '0', ie. one is a string value, the other an actual numeric value. Custom field values are stored as strings, so your comparison should go along the lines of..

if( get_post_meta( $post_id, 'some-existing-field', true ) == '0' )

.. to be accurate.

I realise i'm bad at explaining this, so i hope that helps(or someone else explains it better).

2

To extand Marc Duncans (a.k.a. @t31os) answer:

You multiple ways to question "if ( $some_val_A IS $some_val_B )". There is a difference between equal and identical.

$real_world_ex = get_post_meta( $post_id, 'ratings_users', true );

// Example: test this in your functions.php
function wpse26016_test_vars( $real_world_ex = '' )
{
    $some_int = (int) 1;
    $some_bool = (bool) 1;
    $some_string = (string) 1;

    echo '<pre>';
        echo 'I\'m an integer: ';
            var_dump( $some_int );
        echo '<br />I\'m some boolean: ';
            var_dump( $some_bool );
        echo '<br />I\'m some string: ';
            var_dump( $some_string );

    echo '<hr><strong>Now we can see some comparison<strong><br />';
        echo '<br />I test if integer is <em>equal</em> boolean (notice the 2 "="-signs): ';
            var_dump( $some_int == $some_bool );
        echo '<br />I test if integer is <em>identical</em> boolean (notice the 3 "="-signs): ';
            var_dump( $some_int === $some_bool );
        echo '<br />I test if integer is <em>not equal</em> boolean: ';
            var_dump( $some_int != $some_bool );
        echo '<br />I test if integer is <em>not identical</em> boolean: ';
            var_dump( $some_int !== $some_bool );

    echo '<hr><strong>Now some real world example<strong><br />';
        echo '<br />This is the dump of what's inside your meta data: ';
            var_dump( $real_world_ex );

        echo '<br />I test if integer is <em>equal</em> to your meta data: ';
            var_dump( $some_int == $real_world_ex );
        echo '<br />I test if integer is <em>identical</em> to your meta data: ';
            var_dump( $some_int === $real_world_ex );

        echo '<br />I test if boolean is <em>equal</em> to your meta data: ';
            var_dump( $some_bool == $real_world_ex );
        echo '<br />I test if boolean is <em>identical</em> to your meta data: ';
            var_dump( $some_bool === $real_world_ex );

        echo '<br />I test if string is <em>equal</em> to your meta data: ';
            var_dump( $some_string == $real_world_ex );
        echo '<br />I test if string is <em>identical</em> to your meta data: ';
            var_dump( $some_string === $real_world_ex );
    echo '<pre>';
}

As you can see (when you paste wpse26016_test_vars() in your template), there's a difference between comparing "against value" or type "against value and type".

Use this function always to see if you're doing it right :)

2
  • 1
    Nice, though i personally prefer my print_r's and additionally it tends to make more sense to less code-savy users(i've never found var_dumps to be very readable myself - just a personal thing though i guess).
    – t31os
    Aug 16, 2011 at 15:47
  • You could use var_export( $var, true ); too. Anyway, I want to show the content incl. the var type, so print_r won't bring up too much - var_dump is alittle more suitable in this case :)
    – kaiser
    Aug 16, 2011 at 16:22
1

Based on your clarification, I would recommend something like the following:

<?php 
$rating = get_post_meta( $post_id, 'ratings_users', true );

if ( '0' < $rating ) {
    the_ratings();
}
?>

Alternately, you could use:

<?php 
$rating = get_post_meta( $post_id, 'ratings_users', true );

if ( '' != $rating && '0' != $rating ) {
    the_ratings();
}
?>

But I think the first method will work; and is more concise.

The important thing to note here is that get_post_meta() returns an empty string if the post meta value isn't set.

1

I appreciate all the answers but $post_id was the issue it should have been $post->ID

<?php if(get_post_meta( $post->ID, 'ratings_users', true ) =='0' ) {
}else{
the_ratings();
} ?>
1
  • I wrongly assumed you were defining that variable yourself. Note-to-self, ask the obvious to.
    – t31os
    Aug 17, 2011 at 14:02
0

Try this:

if ( get_post_meta( $post_id, 'ratings_users', true ) != 0 ) {
    the_ratings();
} else {
    // Do nothing here
}
0
get_post_meta($id, 'key', true)
  • returns string if the key exists
  • return false if the key doesn't exists

If you're using wp-postratings or any other plugin that uses custom_fields, they don't create the key until it is used, so you can save time doing:

if(!get_post_meta()) {
    // when field doesn't exist (false) or the value is integer 0 or empty string ''
} else {
    // when the field exists
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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