Skip Navigation

[Resolved] Sorting controls not working as expected

This support ticket is created 4 years, 5 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 21 replies, has 2 voices.

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

Assisted by: Christian Cox.

Author
Posts
#2130787

1) In Find a Native Nanny, the user should be able to sort by our custom field Available From Date (nanny-available-from-date). It appears as if Nanny Ads with Available From Dates in the past are not appearing in the search results if "Available From" is used as the sort criteria.
2) In Find a Job, the user should be able to sort by our custom field Start Date (family-start-date). It appears as if Job Ads with Start Dates in the past are not appearing in the search results if "Start Date" is used as the sort criteria.

It seems there are at least 4 custom code snippets directly impacting these results. I have disabled these in dev for testing purposes:
change_date_filters_for_expiring_nanny_ads (nanny view)
func_set_default_orderby_modified (nanny and job views)
change_date_filters_for_expiring_job_ads (job view)
post_expiry_greater_than_now (nanny and job views)

For issue #1 in the English site, without adjusting any filters, I currently see Available From Dates 9 July - 31 July, which are all in the past.
For issue #2 in the English site, without adjusting any filters, I currently see Start Dates 15 July - 31 July, which are all in the past.

Can you review these results and tell me:
- Are these results accurate now?
- If not, how are they incorrect, and which results should be shown instead?

3) In each of the mentioned Views, in the Elementor Pro Edit Toolset View Widget > Override View basic settings, I have set Order by = modified, and Order = Descending for the View showing the Search results. The suggestions for Order by are "ID, date, author, title, post_type or field-slug"... Can you please confirm that "modified" is a valid argument here (as I'm pretty sure it works when you call the view via a shortcode).
I'll try to verify this with my 2nd tier support team. Please stand by on this issue and I will give you an update as soon as I have some feedback.

#2131819

Hi Christian

Yes, it looks like the results are accurate with those 4 snippets off.

If I'm not mistaken the following two snippets just correct the Toolset plugins not taking timezone offset into account correctly. I'm guessing/(hoping!) I could perhaps live without the two snippets, since our Ads are set to expire 6 months after creation, and the difference of 1 or 2 hours due to daylight saving or whatever we could swallow.
- change_date_filters_for_expiring_nanny_ads
- change_date_filters_for_expiring_job_ads

The 4th snippet
post-expiry-gt-now-view-filter
is also a Toolset provided workaround to the timezone offset bug.

However as you mentioned in your previous update the snippet
func_set_default_orderby_modified
is used to fix another Toolset bug with the display of posts set to display by modified by default, which is our preferred sort critierion for those two Views.

So how do you suggest we can resolve the issue?

Kind regards
Simon

#2132723
Screen Shot 2021-08-03 at 12.20.42 PM.png
Screen Shot 2021-08-03 at 12.19.14 PM.png

So how do you suggest we can resolve the issue?
I see these filters were originally written for use with some other Views (not Find a Job / Find a Nanny Views), and the syntax used to manipulate the query arguments was based on the requirements for those original other Views.

- change_date_filters_for_expiring_nanny_ads
- change_date_filters_for_expiring_job_ads
- post-expiry-gt-now-view-filter

Now when these filters are applied to the Find a Job / Find a Nanny search Views, my guess is the syntax is adjusting the wrong query arguments, and the result is inaccurate filtering. Jamal had some comments about the potential for problems like this in your previous ticket:
https://toolset.com/forums/topic/query-expiring-and-expired-posts/page/2/#post-1789439
"My code, at line 6, assumes that the condition on the time interval is the only condition on meta keys(custom fields). You can see how it uses the index 0:"
Similarly, the code in post-expiry-gt-now-view-filter is written in a way that isn't very flexible to support multiple custom field filters at the same time. Since the Find a Job and Find a Nanny search Views both include custom field Query Filters in addition to the expiration time added by the post-expiry-gt-now-view-filter, the code that manipulates the date filters probably needs to be adjusted to account for these additional custom field filters. Or, if you'd rather not get into adjusting this code, it would be easier to remove the View IDs 2231 and 2145 from these scripts. The scripts would still apply to other Views, so you would continue to get the benefits of these scripts in other places.

4) On a related previous Toolset ticket...we were given a custom code snippet "func-set-default-orderby-modified". Is this issue resolved with the latest updates, and if so, is this custom code obsolete and can now be deleted?
-- No, the latest updates did not address the issue you are referring to in another ticket. It is still in our developers' queue but has not yet been resolved.
func_set_default_orderby_modified - is used to fix another Toolset bug with the display of posts set to display by modified by default, which is our preferred sort critierion for those two Views.
I may have misunderstood the previous ticket and the open issue. I just took a closer look and the issue that is still open in our developers' queue is specifically about View blocks sorting controls. That ticket is still open, but it seems obsolete because right now it seems that the sorting control block functionality has been adjusted in a recent release. Regardless, the func_set_default_orderby_modified function is inactive on your site now, yet the Find a Job and Find a Nanny Views both seem to be sorted based upon Last Modified, by default when the page loads. So I don't think this additional function is necessary anyway, unless I'm misunderstanding something. See the screenshots here. It looks like the sorting is accurate without this function active anyway.

#2135903

Hi Christian

thanks for the deep analysis. I'll have to examine your response carefully and check all the interdependencies of the scripts.

I'll get back to you as quickly as possible.

Cheers
Simon

#2138163

Understood, I will stand by for your updates.

#2138263

Hi Christian

Thanks for waiting. It was a bit complicated analysing all the dependencies and reviewing the old tickets. I'll try to summarise what I've understood:

1) We can safely remove
- View 2145 (Find Nanny) from change-date-filter-for-expiring-nanny-ads
- View 2231 (Find Job) from change-date-filter-for-expiring-job-ads
and still sort these these two Views by Last Modified date and/or by any other custom field.

2) Am I actually correct in saying that these two code snippets are just there to correct the 1-2 hour time difference from GMT/daylight saving, right?
- change-date-filter-for-expiring-nanny-ads
- change-date-filter-for-expiring-job-ads

By the same logic then, it would make sense to remove not only View 2231, but also 1973, 10121 from change-date-filter-for-expiring-job-ads (list below)

// 1973 Edit My Job Ads View
// 2231 Find a Job Search and Results View
// 10351 My Expiring Job Ads View
// 10430 My Expired Job Ads View
// 10121 My Favourite Job Ads View

And similarly, View 2145 as well as 1980, 10151 from change-date-filter-for-expiring-nanny-ads (list below)
// 1980 Edit My Nanny Ads View
// 2145 Find a Native Nanny Search and Results View
// 10720 My Expiring Nanny Ads View
// 10722 My Expired Nanny Ads View
// 10151 My Favourite Nanny Ads View

Correct?

3) Snippet post-expiry-gt-now-view-filter (currently inactive) came into existence in another ticket with you: https://toolset.com/forums/topic/_cred_post_expiration_time-is-not-respecting-timezone-or-problem-with-view-query/#post-1939421

Has this issue been resolved/improved by your dev team yet? Looking back on that ticket, it looks like I created a "greater than" version of the code you provided (my version: post-expiry-gt-now-view-filter) probably because Expired Ads may have been popping up in other Views (list below):
// 1980 Edit My Nanny Ads View
// 1973 Edit My Job Ads View
// 10151 My Favourite Nanny Ads View
// 10121 My Favourite Job Ads View
// 2145 Find a Native Nanny Search and Results View
// 2231 Find a Job Search and Results View

The "less than" code snippet you provided in the ticket (my version: post-expiry-lt-now-view-filter) was only for Ads that had actually expired, right?

4) func_set_default_orderby_modified
Looks like this one can be deactivated. It may be related to changes in more modern versions of the plugin, where I believe "modified" became a valid argument, whereas in previous versions, one could only use "date" as an argument? Any more updates from 2nd level on the "modified" syntax? https://toolset.com/forums/topic/sorting-controls-not-working-as-expected/page/2/#post-2130787

Thanks and regards
Simon

#2139227
Screen Shot 2021-08-10 at 9.50.07 AM.png

1) We can safely remove...View 2145...View 2231...from change-date-filter-for-expiring-job-ads and still sort these these two Views by Last Modified date and/or by any other custom field.
Yes, I think so. I don't see any problem with that. The front-end sorting controls should still continue to function as expected without the custom filtering code applied to these Views.

2) Am I actually correct in saying that these two code snippets are just there to correct the 1-2 hour time difference from GMT/daylight saving, right?
Based on what I can see in the other ticket, it looks like these two code snippets were designed to help filter out expired posts by adjusting the calculated filter expiration time to account for the potential time difference between the expiration field (GMT/UTC) and the calculated filter time (server timezone). The original View 10351 had one custom field Query Filter based on the post expiration field. The code adjusted the expiration date calculated in the filter using NOW or SECONDS_FROM_NOW.

By the same logic then, it would make sense to remove not only View 2231, but also 1973, 10121 from change-date-filter-for-expiring-job-ads...And similarly, View 2145 as well as 1980, 10151 from change-date-filter-for-expiring-nanny-ads (list below) Correct?
That would leave only two Views in the list of filtered View IDs for change-date-filter-for-expiring-job-ads: 10351 and 10430.
10351 no longer exists:
hidden link
10430 has no expiration date filter, as far as I can tell, unless it is added in other custom code:
hidden link

It would leave only two Views in the list of filtered View IDs for change-date-filter-for-expiring-nanny-ads: 10720 and 10722.
10720 no longer exists:
hidden link
10722 has no expiration date filter, as far as I can tell, unless it is added in other custom code:
hidden link

So for all practical purposes, the change-date-filter-for-expiring-job-ads and change-date-filter-for-expiring-nanny-ads filters can be deleted. They are not doing anything effectively now. They would only be effective if any Views in the list of IDs had one custom field filter for the expiration date field _cred_post_expiration_time, with the field value calculated using NOW(), and that is not the case for any of the Views included in the list of IDs.

3) Snippet post-expiry-gt-now-view-filter (currently inactive) came into existence in another ticket with you...Has this issue been resolved/improved by your dev team yet?
Well no, the problem is the same problem we are addressing in this ticket. My solution was for displaying expired posts correctly and Jamal's solution is for filtering out expired posts correctly. They are opposite sides of the same problem. I have attached a screenshot to explain the problem again. To show or hide expired posts, you need to filter a View based on the field _cred_post_expiration_time. A Query Filter should be configured based on that field. To show expired posts, the value must be lower than NOW(), and to hide expired posts the value must be greater than NOW(). The root problem is that NOW() is calculated incorrectly by Views filters.

Jamal's solution was designed to work with a Query Filter that exists in the View editor screen. It adjusts the calculated NOW() value automatically. My solution was designed to work without a Query Filter in the View editor screen. It creates a filter programmatically and compares the value of the expiration field to the correct value of NOW(). No Query Filter for this field will appear in the View editor screen.

The "less than" code snippet you provided in the ticket (my version: post-expiry-lt-now-view-filter) was only for Ads that had actually expired, right?
Yes, the filter there was designed to display ads that have expired.

I believe "modified" became a valid argument, whereas in previous versions, one could only use "date" as an argument? Any more updates from 2nd level on the "modified" syntax?
Yes, the option to orderby "modified" will sort the results based on the last modified date. I think you can deactivate that snippet for now, or delete it.

#2143737

Thanks for all the detailed analysis, Christian! My issue is resolved now. Thank you!

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