0

I have the below GET function that is based on regular WordPress custom field names. When ticked, it sorts all posts that have that custom field value set to 1. It currently works. But, I happen to have two custom fields named: 'free' and 'twofree'

When I tick 'free', it also includes 'twofree' and vica-versa. It does not seem to be case sensitive. Is there any work around to this?

<?php 
/* my code starts from  here*/
 if( isset( $_GET['show'] ) && !empty ( $_GET['show']  )  ){
   if( $_GET['show'] == 1 ) 
    $meta_field = 'free';
  else  if( $_GET['show'] == 4 )
    $meta_field = 'sale';
   else if( $_GET['show'] == 2 )
    $meta_field = 'genuine';
    else if ( $_GET['show'] == 'onfire' )
    $meta_field = 'onfire';
    else if( $_GET['show'] == 5 )
    $meta_field = 'twofree';    
 else if( $_GET['show'] == 3 )
    $meta_field = 'onfire'; 

        if( $_GET['show'] == 'sale' )
            query_posts('cat='.$cat.'&meta_key='.$meta_field.'&meta_value > 0');
        else
          query_posts('cat='.$cat.'&meta_key='.$meta_field.'&meta_value>=1');   
 }
/* my code ends from  here*/ 
?>

EDIT: I have found the problem and it lied in the part

query_posts('cat='.$cat.'&meta_key='.$meta_field.'&meta_value>=1');

I changed it to

query_posts('cat='.$cat.'&meta_key='.$meta_field.'&meta_value=1');
user1083148
  • 109
  • 13

1 Answers1

2

Use identity operator === when you want to match exact values, instead of == which checks similar values based on strings/integers.


More one this subject, check out this linkor this answer below

Using the `==` operator (*Equality*)

    true == 1; //true, because 'true' is converted to 1 and then compared
    "2" == 2 //true, because 2 is converted to "2" and then compared

Using the `===` operator (*Identity*)

    true === 1 //false
    "2" === 2 // false

This is because the **equality operator == does type coercion**...meaning that the interpreter implicitly tries to convert the values and then does the comparing.

On the other hand, the **identity operator === does not do type coercion**, and so thus it does not convert the values of the values when comparing
Community
  • 1
  • 1
samayo
  • 16,163
  • 12
  • 91
  • 106
  • This has nothing to do with the question. This is also an incorrect description of what the `==` operator does. `==` will perform _type coercion_ (not conversion) before comparing the values. This means that `false` and `0` are `==` because they are equal when compared as the same type. `===` however, respects types and does no such coercion. Therefore, false `!==` 0 (since one is boolean and one is an integer). Neither one will make `"free" == "twofree"` nor will it change case – Colin M Apr 03 '13 at 13:34
  • Maybe I don't understand his question clearly, but his title seems to suggest that he is trying to compare two values, which are case sensitive and yet evaluating to same value when computed with == operator – samayo Apr 03 '13 at 13:40
  • `==` does not convert case when comparing. "THIS" `!=` "this". It _only_ performs type coercion before comparing. Nothing else. It's not an issue with his code. Database values in MySQL (which I assume is backing this) are not case sensitive. It's likely part of WP – Colin M Apr 03 '13 at 13:42
  • I found the cause of the issue. It was the query_posts('cat='.$cat.'&meta_key='.$meta_field.'&meta_value>=1'); that i changed to &meta_value=1'); instead. Sorry for using up anyone's time. But the issue is now fixed. – user1083148 Apr 03 '13 at 13:45