Make WordPress Core

Opened 7 years ago

Last modified 7 years ago

#41546 new defect (bug)

WP Query order by meta_clause not working correctly for decimal values

Reported by: lordspackolatius's profile LordSpackolatius Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.2
Component: Query Keywords:
Focuses: Cc:

Description

Short description: When using the WP 4.2 improvements for meta queries (as described here: https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/) and trying to order the query by a decimal custom field, wordpress orders as if these fields are all integer and incorrectly handles all decimal values such as 1.5, 1.6 and 1.7 (all of these are seen as 1).

In detail:

WP_QUERY_ARGS Array
(
    [ignore_sticky_posts] => 1
    [post_status] => publish
    [offset] => 0
    [post_type] => Array
        (
            [0] => post
        )

    [orderby] => Array
        (
            [cf_clause_1] => DESC
            [ID] => ASC
        )

    [meta_query] => Array
        (
            [cf_clause_1] => Array
                (
                    [key] => my_decimal_custom_field
                    [type] => DECIMAL
                    [compare] => EXISTS
                )

        )

    [posts_per_page] => 10
    [nopaging] => 
)

5 Test posts:
Post 1 - my_decimal_custom_field = 1
Post 2 - my_decimal_custom_field = 2
Post 3 - my_decimal_custom_field = 2.2
Post 4 - my_decimal_custom_field = 2.6
Post 5 - my_decimal_custom_field = 3

Using the query above results in the order:
1,3,4,2,5

When it should be 1,2,3,4,5.

Trying out different order types as described in the WP Codex does not help.

Change History (4)

#1 @LordSpackolatius
7 years ago

  • Summary changed from WP Query order by meta_clause NUMERIC not working correctly for decimal values to WP Query order by meta_clause not working correctly for decimal values

#2 @LordSpackolatius
7 years ago

I made a small mistake with the 5 example posts:

Here is the correction:
Using the query above results in the order:
5,2,4,3,1
When it should be 5,4,3,2,1

#3 @subrataemfluence
7 years ago

A quick suggestion, might be useful!

What actual SQL query you get when you extract it using $query->request; and print it on screen? Does this outputs the query exactly as you want?

If yes, what you get when you run this query directly on your SQL query browser? Does this output the values in correct order?

Last edited 7 years ago by subrataemfluence (previous) (diff)

#4 @Clorith
7 years ago

  • Version changed from 4.8.1 to 4.2
Note: See TracTickets for help on using tickets.