trying to do if post meta !=0

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.

Topic rating post-meta Wordpress

Category Web


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
}

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();
} ?>

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.


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 :)


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).


Try this:

if ( get_post_meta( $post_id, 'ratings_users', true ) != 0 ) {
    the_ratings();
} else {
    // Do nothing here
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.