Skip Navigation

[Resolved] Sorting with Multiple Keys

This support ticket is created 2 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 11 replies, has 2 voices.

Last updated by justinl-5 2 years, 8 months ago.

Assisted by: Minesh.

Author
Posts
#2425109

Hello,

I have a page built on Oxygen and I used Toolset to create some custom fields:
hidden link

I want the data to be sorted by 2 custom fields: NL Date (ASC) and Used As (ASC). The "Used As" field is stored as either 1 (for Primary) or 2 (AOTW) in the database. So the result I expect is that the data show up like this:
2022.07.01 Wed Primary
2022.07.01 Wed AOTW
2022.07.02 Thu Primary
2022.07.02 Thu AOTW
and so on...

I reached out to Oxygen support and they helped me to build the sorting part of the query:
<?php
function oxy_reorder_repeater($query) {

global $post;

if($query) {
$query->set('meta_query', array(
'relation' => 'AND',
'nl_clause' => array(
'key' => 'wpcf-last-used',
'compare' => '>=',
'value' => getBeginDate(),
'type' => 'numeric',
),
'nl_clause_2' => array(
'key' => 'wpcf-last-used',
'compare' => '<=',
'value' => getEndDate(),
'type' => 'numeric',
),
'used_clause' => array(
'key' => 'wpcf-used-as',
),
));
$query->set('orderby',array(
'nl_clause' => 'ASC',
'used_clause' => 'ASC',
));
}
}

add_action('pre_get_posts','oxy_reorder_repeater');
?>

As you can see, it works fine for July 2022. But if you select Aug 2022, you'll see that "2022.08.22 Mon Primary" is placed incorrectly. It's at the bottom of the list, instead of before "2022.08.22 Mon AOTW". It happens in the other months as well.

I have updated Oxygen with this finding and they have investigated further. But they were unable to find a solution. And since the custom fields are created with Toolset, they asked me to reach out to you to see if you can help with the correct wp_query. Can you please look into this and help me with a solution to fix the issue? Thank you.

#2425205

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

I would like to know that did you build custom search view to filter the results?

#2425389

Minesh
Supporter

Languages: English (English )

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

Could you please make sure that you are loggedin currently or try to logout and login again and check if that helps to reply you in this ticket.

#2426119

Ok, I just realized that looks like I need to click "I still need assistance" in order to reply. This is a very bad UX. Not intuitive at all. Anyway, to answer youe question, I didn't build custom search view to filter the results. The page is built on Oxygen and I just used Toolset custom fields.

#2426129

Minesh
Supporter

Languages: English (English )

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

Based on the query arguments shared by you, Types field store date value as Unix Timestamp and you are using the correct field type with the "type" argument as "numeric" with the query argument.

Would you mind to install the query monitory plugin:
- https://wordpress.org/plugins/query-monitor/

Then, check what query is build when you see the correct result and what difference you notice with the query when you change the filter. Actually it's a custom code and we offer limited support for such custom edits but if you notice that Toolset is the core issue here (which should not be as you are using the standard WP hook "pre_get_posts" to get filtered results) I'm happy to help you further.

#2427063

Ok, I'll try that out. But I think I won't be able to do it this week. Will do it and update you early next week. Please keep this ticket open. Thank you.

#2427137

Minesh
Supporter

Languages: English (English )

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

ok Ticket will be open for one week, if it will get close, you can create a new one and share reference link to this ticket.

#2428569

Hi Minesh,

I have installed the plugin and compared the query for July (correct result) and Aug (incorrect result). I don't see anything different, but I might look at the wrong place because I don't know exactly what you wanted me to look for. Please take a look at it by yourself and let me know what you think. Thank you.
hidden link

#2428787

Minesh
Supporter

Languages: English (English )

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

You will have to compare the query when you select July 2022 (that works fine) against August 2022 and check difference for both queries.

This is again custom code, if you will require any help with such custom edits, you are welcome to contact our certified praters:
=> https://toolset.com/contractors/

#2430895
1.png

I already did that but I don't know if I looked at the right query because there are a lot of them. Pls see the attached image for July vs August query. As you can see, there is no difference except for the timestamp, which is as expected.

I forgot that you need to log in to see the query by yourself. Is it safe to share the login info here?

#2430905

Minesh
Supporter

Languages: English (English )

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

I'm afraid that this is a custom code and we are not allowed to offer support for such custom query edits.

To get help for such custom edits, you are welcome to contact our certified praters:
=> https://toolset.com/contractors/

#2430911

Ok, thank you.