Skip Navigation

[Resolved] Oder by score

This support ticket is created 8 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 1.13 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Tagged: 

This topic contains 6 replies, has 2 voices.

Last updated by Beda 8 years, 5 months ago.

Assisted by: Beda.

Author
Posts
#347262

Hello

I have a SELECT custom filed called "score". It has 10 options:

1,2,3,4,5,6,7,8,9,10

I want to sort posts by the above custom filed from highest to lowest.

My query options:

- Order by: "score" custom field
- The custom field "score" is a number that is between 0 and 10

However, the result isn't correct. It works fine between 1 and 9 but when there is a post which has 10 score the result will be like this:

9
8
8
7
6
5
5
4
3
3
3
2
1
10

What is the problem?

Thanks

#347404

Thank you for contacting us here in the Support Forum

I have reproduced the setup but not the problem.

1. Set up a Custom Field NUMERIC "Score"

2. Poplaute 10 posts fields with values, 1 to 10, numeric

3. Created a View, and ordered by that custom field "Score" ascending and descending

this works great on the front end.

Please can you ensure you use a NUMERIC Field and perhaps try to resave the posts / View as well

Please don't hesitate to inform me in case the issue persists

Thank you

#347409

Hello Beda,

> 1. Set up a Custom Field NUMERIC "Score"

It isn't a numeric field. I have a SELECT custom filed with defined scores.

#347537

I see.

It is not recommended to sort by a non numerical Field, but since you most probably store numerical Values to the Field (so I did locally) this should basically work the same.

It seems, Views is only taking into account the FIRST value of a number, so if you set a value 11 or 12, it will only take into account 1, 1, 1, instead of 11, 12, 13.

I have reported this to the DEV team to take a look and provide a Fix in a next release.

Meanwhile, I can suggest to use a Numeric Field, which will work as expected, but I understand that this is not the same UX as a Select Field with limited possibilities to choose from (1 to 10).

Please do not hesitate to open a new thread if other issues or problems arise

Thank you for your patience.

#347541

Hello Beda,

Please can you provide any temporary solution?

Thanks

#348584

I apologize the delay here

I suspected it but I wanted to be sure first.

The 2nd Tier confirmed it.

It's not actually a bug.

The sorting is actually correct since it's sorting as string and not as integer in this case.

That is the default way wp_query does the order by clause.
"1" does come before "12" that does come before "2" (if as string)

The numeric field is explicitly, well, numeric.
Here 1 comes before 12, and 12 is considered a NUMBER, there fore it comes after 2.

This allows Views to force wp_query to compare data using a "numeric" way.

Since the select element can use ANYTHING on the value parameter, Views would have to guess that these values are numeric so the comparison can be forced to NUMERIC in wp_query.

But Views can not guess this, as we do allow all values in a SELECT Field, not so in a NUMERIC Field, which is explicitly numeric.

To conclude, you would have to use a NUMERIC field here, because WordPress Query itself, will otherwise look at the value as a String, instead of a Numeric Value.

Please do not hesitate to open a new thread if other issues or problems arise

Thank you for your patience.

#349214

I wanted to update here, that the DEV is considering a new feature for future Views Versions where:

1. When sorting by a custom field, we offer a way to sort as string or numeric values.

That would then allow you to use your initial approach.

But I can not state any ETA on this feature.

When exactly our developers will add a new feature or not is not up-to us Supporters to determine.
Each issue reported in, whether a bug or a new feature request, is taken seriously into consideration.
It is given a high or low priority based on a number of factors.
Factors which those with the overview of all things related are in the best position to determine.
I apologize any inconvenience this may cause.

Please do not hesitate to open a new thread if other issues or problems arise

Thank you for your patience.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.