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.

im trying to do a simple if function on my code but for some reason it just isnt working correctly, ive gone over it several times to see if there is anything im missing but no luck, im 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 ive tried 3 different ways to get this stupid thing to output nothing if the value in the custom field is 0 and still it doesnt work correctly

share|improve this question
    
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. –  Chip Bennett Aug 16 '11 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 '11 at 15:30
add comment

6 Answers

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

share|improve this answer
add comment

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

share|improve this answer
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 '11 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 '11 at 16:22
add comment

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.

share|improve this answer
add comment

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();
} ?>
share|improve this answer
    
I wrongly assumed you were defining that variable yourself. Note-to-self, ask the obvious to. –  t31os Aug 17 '11 at 14:02
add comment

Try this:

if ( get_post_meta( $post_id, 'ratings_users', true ) != 0 ) {
    the_ratings();
} else {
    // Do nothing here
}
share|improve this answer
add comment
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
}
share|improve this answer
add comment

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.