Goal :  I want to search item/post/product’s which price between two predefined amount using meta query.

Default Functionality :- As per default WordPress functionality, All the post or post type’s  custom field value stored in Wp_postmeta table in database. So as per our default functionality , the item/Posts/product’s price is also stored in same table with meta_value field. (meta_value field datatype is “logtext” as default).

For this reason , all the value is stored in string format. now when i tried to search price with meta_value field then it is not working . Always system is getting blank result.

Problem :- Numeric value is not search in meta_value field using meta query.

Solution :-  We need to use “type” parameter in meta_query .Type contains  values are ‘NUMERIC’, ‘BINARY’, ‘CHAR’, ‘DATE’, ‘DATETIME’, ‘DECIMAL’, ‘SIGNED’, ‘TIME’, ‘UNSIGNED’. Default value is ‘CHAR’.

 Without use of  Type Parameter : – 

After Using Type Parameter : – 

The 'type' NUMERIC works with the 'compare' value BETWEEN  if the price is stored at the format string format .

When you used type parameter in meta query then one function is automatically add in your query which function name is CAST().

Using the CAST() function converts a value of any type into a value that has a specified type. The target type can be any one of the following types: BINARY, CHAR, DATE, DATETIME, TIME,DECIMAL, SIGNED, UNSIGNED

 

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...