Skip Navigation

[Resolved] Views orderby not accepting formats like 1:2.4

This thread is resolved. Here is a description of the problem and solution.

Problem:
When I order by a Custom Field that holds numeric values like 1:2.4 then the orderby fails.
It produces unexpected results.

Solution:
This is expected.
Numbers should be in a numeric format, like integer numbers or separated by an accepted delimitation like a dot.

This support ticket is created 6 years, 9 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.

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)

This topic contains 21 replies, has 4 voices.

Last updated by vijayB-4 6 years, 8 months ago.

Assisted by: Beda.

Author
Posts
#555046

I have a website for a swimming club. As a part of the website we store timings of swimmers as a CPT which is a child to the swimmer and the event. The swim races are a taxonomy.
hidden link

I now wish to create another view which displays the personal best of the swimmer for each swim race type which can be displayed in the swimmer's page.

Hence there are two queries The first is to collect the unique number of races each swimmer has participated in and the second query is to find the lowest timing for each race type (Taxonomy) and display the same

Can Views help me do this?

#555071

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - for that I need to review your current structure.

Hence there are two queries:
The first is to collect the unique number of races each swimmer has participated in:
=> how you are storing this data to which races swimmer participated - as event post type? I mean how we can categorize the swimmer participation using "Event Name" or "Swim Races"?

and the second query is to find the lowest timing for each race type (Taxonomy) and display the same
=> this needs taxonomy filter.

#555082

Hi Minesh,

The Swim Times are stored with the following fields
1. Race Type - As a Taxonomy
2. Swim Time - Custom Field - Text Field
3. Race Position - Custom Field - Select Field
4. New Meet Record - Radio Button
5. Swimmer Name - PArent Child Relationship
6. Event Name - PArent Child Relationship

We categorize swimmers based on Race types

for example swimmer 1 swims 5 races
100 m Freestyle
50 m Freestyle
200 m Freestyle
50 m Breaststroke
50 m backstroke

Vijay

#555094

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can I have one test case example based on that I will try to build the solution for you but first of all I need to review your current setup.

Please share one text example of your expected output and problem URL.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#555589
Bildschirmfoto 2017-08-02 um 17.53.17.png

There is a htaccess Login Form to reach the Login form of WordPress, for which you did not provide the login data.
(See ScreenShot)

Anyway, I do not think this is possible.

How would you compare the values of "the lowest" or the "highest"?

This can be done (even in PHP) only if you have a reference value.
Like, "If current_value is bigger than 1".

But "The highest" is not really something available.

What you can do is crate a View, and order that View by the values of that Field, hierarchically, and then output just one item.
That will display the highest score (or the lowest, if you order ascending).

That should be straight forward and possibly the only solution to your issue.

Thank you

#555593

Hi,

The login details are the same as the admin access for WordPress

The values are timings stored as text (so essentially the highest value is the maximum time say 16:15.08 and lowest would be the lowest time say 16:23.18) This would change for each taxonomy

Secondly each swimmer does not participate in each race type i.e. taxonomy hence we need to display only taxonomies which a swimmer participates in.

Vijay

#555604

The login details are the same as the admin access for WordPress

I apologise, but they are not.
I tried them before I contacted you, and once more now.

Can you re-check them?

I enabled a private reply.

But again, this is not possible.

(so essentially the highest value is the maximum time say 16:15.08 and lowest would be the lowest time say 16:23.18

This cannot be sorted for sure as text, and also not as number as those are not numbers but mixed values.

It would need to be either a floating number or a simple string.

You can sort Posts or Taxonomies by Custom Fields, but only in the first order by option, and such a mixed value will give completely unrelated results, as it is a string with numbers.

I apologise that in this format you cannot order a view.

#555951

What should I look at :)?

I explained this would not work.
https://toolset.com/forums/topic/can-toolset-help-me-with-this-problem/#post-555604

Those values cannot be used to sort a View, not even in plain PHP.

I am sorry, you need to re-factor the values of those Fields.

They need to hold (for your goal to be achievable) a numeric data like 1, 2, 3 and so on.
Or, eventually a "1,32", "1,55" and so on.
But 1:22.3 will securely fail.

Anyway, I fiddled a bit with the View "Personal Best of Swimmers"
I query the Post Type "Swim Timings" as that is what you had set.
I order by the field "Swim time"
I output in the Loop the title and the field value.

As you see here (your-site.com/toolset-test-page/) the field has no values to output at all.
That is because you cache your Website So heavily it requires you to use a View non-cache attribute in the View ShortCode:

[wpv-view name="personal-best-of-swimmers" cached="off"]

Then you can see the values.

But as you see the order is not respecting those Fields values and that is expected, as explained above concretely.

You need to overthink the structure. Such a value cannot be used in any Code to order by.

#556875

Hi Beda,
Thanks for your response. The problem is that in the sport times are stored in milliseconds. Is it possible for me to say change the field from plain text to a date field?

Vijay

#556925

Hi Beda,

Do you think it can work if we break down the time into say 2 fields one for minute which is an integer and one for seconds and milliseconds (19.26).

Can we then look at the possibility? Further will it be possible to then look at best timings for each swimmer for each taxonomy.

hidden link

So essentially what I need is that for each swimmer on his/her page we publish the lowest time for each taxonomy.

Vijay

#557609

The problem is that in the sport times are stored in milliseconds.

To me, this does not seem to be the case.
And it was what I was going to suggest:
Store the data in the smallest Unit:
Milliseconds.

But that gives us this format:
1 minute == 60000 milliseconds.

Hence, 16:23.18 minutes is not a milliseconds value.

Then, if you store the value in milliseconds, it will work, because it becomes a real number.

Storing the data in many different fields is OK but will not let you order by them, as the secondary ORder by will not accept Custom Fields, and the Primary -Order-By takes only one argument.

To order your posts correctly, you need a unique numerical value, stored in one Custom Field.
That should be a value like "6000" for one minute, not 1:00 or 1:00.00

Thanks!

#558469

Hi Beda,

Thanks a lot for your response. This has got me thinking. So is it any way we could write a short code wherein the data is stored in Milliseconds but the input/ output is in seconds. If this could be done then i think we have a solution.

But the second part of my problem is to display the best times across swim races. How can we go about that.

Vijay

#558492

You can store the Milliseconds in a numeric Format in a Numeric Types Field, for example.

Then, to Display that as seconds you need Custom Code.

One idea is to get the value of that Custom Field and transform it.
We know that one Second has a 1000 Milliseconds, so it is an easy mathematical calculation you need to do here:
Milliseconds/1000 == Seconds.

The ShortCode you write should use get_post_meta() to get the Fields value or accept an attribute where you can put the shortcode for that field.
Then, in the Shortcode, use that value, divide it per 1000, and return the result.
Insert this ShortCode in your View to display it

For the other issue, I suggest opening a new ticket dedicated to the exact issue, after you have solved this issue here.

That will allow concisely and to the point solution.

Shortcodes are written using the WordPress API:
https://codex.wordpress.org/Shortcode_API

#558691

hi beda,

Point taken

First I shall try to use the CRED form to input data

Now new issue

I have changed the swim-time custom field to numeric. I am unable to disable the validation for the number.

I have written code to convert the string to milliseconds

add_action('cred_save_data', 'msconvert', 10, 2);

function msconvert($post_id, $formdata){
if ($formdata['id']==11856 or $formdata['id']==11862)
{

$value = get_post_meta($post_id, 'wpcf-swim-time', true);
$keywords = preg_split("/:.]+/", $value);
$ms = $keywords[1] * 1000 + $keywords[0] * 60 * 1000 + $keywords[2] * 10;
wp_update_post(array('ID' => $post_id, 'swim_time' => $ms));
}
}

Now awn I try to add data through the CRED form I get the following error
The post was not saved because of the following problem:

And nothing under it except a box on top of the swim time field. I believe this is because it expects a numeric input.

Please assist
Vijay

#558868

Yes, Numeric Fields expect numeric Input.
Milliseconds without any comma or point are numeric.

If you want the user to enter values like 1:23,3 or so, and then update a Numeric Field with the converted data, you need to approach it differently

1. Remove the actual numeric Post Field from the CRED Form
2. Add a generic Single Line Field
3. Get the data from the $_GET form data (address the generic Field)
4. Update with update_post_meta() the actual Custom Field created with Types.

This way, you can enter whatever you want in the Generic Single Line field (this will never be saved) and then get that data with $_GET and update_post_meta() the actual numeric field in your CRED save_data function.

Does it makes sense to you?

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