Opened 5 months ago
Last modified 3 months ago
#23033 accepted defect (bug)
Decimal and numeric options in meta_query do not produce correct MYSQL for floating point numbers comparisons
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Priority: | normal | Milestone: | 3.6 |
| Component: | Query | Version: | |
| Severity: | normal | Keywords: | has-patch dev-feedback |
| Cc: | ADAMSILVERSTEIN@…, knut@… |
Description
If you have a custom post type (shoes) that has floating point numbers (shoe size) as post meta, querying against this post meta with a specific decimal value ( >10.5 ) does not work properly because of the way the values are cast out of the database, and will produce surprising results.
Attachments (2)
Change History (8)
comment:1
SergeyBiryukov — 5 months ago
- Component changed from General to Query
comment:2
follow-up:
↓ 4
wonderboymusic — 4 months ago
- Keywords needs-patch added
- Milestone changed from Awaiting Review to 3.6
- Owner set to wonderboymusic
- Status changed from new to accepted
comment:3
adamsilverstein — 4 months ago
- Cc ADAMSILVERSTEIN@… added
- Cc knut@… added
Replying to wonderboymusic:
We should probably be casting the value (10.5) so the query doesn't end up = (string) '10.5'. We should also support FLOAT.
Cast 10.5 to 10.5 is meaningless. The value is already a float. The problem seems to be that the value is converted to a string when it shouldn't.
We'll also need to be able to specify the precision and scale for fixed-point data types. I suggest that we use the convention of defining the precision and scale in the 'type' field, and explode the string for parsing when we need to.
$stuff = new WP_Query( array( 'meta_query' => array( array( 'key' => '_edit_last', 'type' => 'decimal(3, 1)', 'compare' => '=', 'value' => 10.5 ) ) ) );

Our support for CAST needs work. Here is what you have to do:
We should probably be casting the value (10.5) so the query doesn't end up = (string) '10.5'. We should also support FLOAT.