Reputation: 109
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');
Upvotes: 0
Views: 570
Reputation: 16495
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
Upvotes: 2