Skip Navigation

[Resolved] Secondary Sorting is not working

This support ticket is created 5 years, 2 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.03 hours from now. Thank you for your understanding.

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)

Tagged: 

This topic contains 7 replies, has 2 voices.

Last updated by antonioV 5 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#1334275

Hello,
I was using the secondary sorting in views until last update was working, now I select one custom field ''featured'' for my primary sorting and when I select the secondary as "order by date" doesn't work.
Only shows me the primary sorting and if I don't have any appears a message saying that there is no items found.
I tried to do a new view and I still have the same problem.
Kind Regards,
Antonio

#1334471

Only shows me the primary sorting and if I don't have any appears a message saying that there is no items found.
Hi, if you try to sort by a custom field but a post does not have a value for that field, it will not appear in the View results. This is expected, because of how WordPress handles meta queries, even with a secondary sorting option like post date. However, if the secondary sorting is not working when results are displayed, that indicates a problem. Is it possible for me to see the results of this View on the front-end of your site? If so, please provide a URL. If login is required, please use the private reply fields here.

#1334659

If I don't have any primary sorting "featured" the secondary sorting by date doesn't appear (having posts for the secondary sorting).
Yes, as I said, this is a known quirk in how WordPress query sorting works. If the primary sorting field isn't set in some post, it will never appear in the results - secondary sorting is irrelevant.

As far as I know, that has always been the case so I'm not sure how this could have been working before the update unless I'm missing something important, or the sorting algorithm implemented by WordPress has changed significantly since your last update. Can you tell me more?
- Did you keep a backup of the site from before the update? If so, you can disregard the following questions. I would like to see that backup to understand what's happening here. Let me know in what format you created your backup.
- Was the option "save 0 to the database" selected for the Featured Ad checkbox field?
- Did you have custom code in place that modified the query using wpv_filter_query or another of our Views APIs?
- Can you tell me what software versions you used before the update?

#1339097
70659083_890276561342891_6497494149648875520_n.jpg

I had a custom field named ''featured'' and I had secondary sorting for post by date, and I had the order right.
I had them working as you can see in this photo was taken in 20 of June 2019. Now if i choose ''featured'' first the posts that doesn't have the ''featured'' field don't appear at all and before the update i had them working.
Unfortunately I don't have any backup from that time, I didn't change the option "save 0 to the database"
The only custom code that I insert in toolset custom code was the one given by toolsets documentation regarding the message system but I believe that didn't change the views... Unfortunately I'm not sure which version of views I had at the time but was the last version that was currently in June 2019.
If the secondary order doesn't work how can I order the view, to have ''featured'' first and then posts by date as I'm building the classifieds website I need to be able to use that feature.
Kind regards

#1340553

One way to implement this type of sorting is to use a radio button instead of a checkbox field to indicate whether or not a post is featured. This approach can be handled from wp-admin without any custom code. Create a new radio button custom field. Set the default option of the radio field to be not featured, set the field to be "required", then resave each ad post to set the correct value for the Featured radio field. Now all the ad posts will have some value for the featured field, and sorting will work correctly with the date fallback. Update the View's sort order and then you can delete the old checkbox field. Another ticket discussing this can be found at the following link: https://toolset.com/forums/topic/featured-ad-placed-at-the-top-of-the-lists/

Another way is to set "save 0 to the database" in the field settings, but this has other implications. If you choose this option, it may not be possible to filter a View using this checkbox field. We do not recommend this approach anymore, but you can see another similar ticket here: https://toolset.com/forums/topic/show-posts-with-a-checked-custom-field-on-top-of-view-parametric-search/

This ticket describes a very similar situation and the author is "Antonio" - just a coincidence? It seems like a similar problem was discussed several years ago and "save 0 to the database" was involved at the time: https://toolset.com/forums/topic/order-by-two-or-multiple-parameters/

Another way is to use custom code. We offer several PHP API filters you can use to manipulate the query criteria on-the-fly, or manipulate the result set for a View: https://toolset.com/documentation/programmer-reference/views-filters

In my opinion, the radio button solution is best. If you already have a significant amount of content and you cannot effectively resave each post individually, consider a custom database script to set these values automatically, or a 3rd-party plugin that will allow you to set some custom field values using Bulk Edit.

#1344879

I did like you said with the radio button.
I have a ''featured'' option and a ''free'' option so now how can I choose which one the view will display first?
I have the order settings for the featured (radio) .
Thanks in advance

#1345533
Screen Shot 2019-09-22 at 10.10.00 AM.png

If your radio option Custom field content values are "featured" and "free" as shown in the screenshot, then you can sort Ascending, as a string, to show "featured" posts first. That's because "featured" is before "free", like in a dictionary.

#1348175

My issue is resolved now. Thank you!