Skip Navigation

[Resolved] View sorting by custom field 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.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 25 replies, has 3 voices.

Last updated by lenas 5 years, 2 months ago.

Assisted by: Nigel.

Author
Posts
#1350831
2019-09-28 06_30_49-Window.png

I am trying to get the Toolset view to sort my products list by a toolset custom field called "new" and this is not working (no matter if it's set to string or number, etc)

#1350855

The view hidden link indeed orders by the field "new".
You need to set the string or number according to what it is. Is it a string or a number?
It seems a number, precisely 1 or 0, and hence cannot really be ordered by that field, because there will be exactly as many 1 and 0 posts as you save and there are more than just 2 posts to display.
What you need is, if you want to group by new and not new (first new, then old) is order by that field, make sure that EVERY product has the field saved and then add a secondary order option - this is for all the posts that have the same value, you still need to order them somehow - so I'd take post date here.

This means your view should order the products properly when you set the Custom Field "new" ordering by number (one or 0) and then a secondary orderby for post date.

If this does not solve the issue or does not address it, can you elaborate on what you mean by not working?
Is the View not showing all results, none, or all but not ordered right?

Could you also add the links and steps to take on your site for someone not familiar with the setup?
I see the views you have, but hidden link, hidden link are both ordering by post date, and the only ordering by that field is as above elaborated, I hope I caught the right one

Thanks!

#1350861

Thank you for the quick reply!

Here are some more details:

The view that is being used is this one: hidden link
The custom field that is being used is here: hidden link
The New checkbox custom field was initially set to give a 1 when checked and do nothing when not checked.

When I added the sort filter in the View to by custom field New, it only gave me products that have New checked. I have changed the logic of the field to set a 0 whenever New is not checked and have gone into the database and ensured a few products have wpcf-new_product meta_key set to 0 in the wp-postmeta table.
I know see that all the products that have any wpcf-new_product defined are showing, however they are not sorting. The sort is set by Number.

I am expecting to see all products that have wpcf-new_product to show here: hidden link with the New products first, which is identified by a round New image on top of the product image.

Hope this clarifies better.

thank you

#1350921

Hence I guessed the right View.

I see that is ordered correctly but probably just accidentally, only new products on top, only others on bottom.
hidden link

Note that you cannot use the Checkbox Field in Toolsets Views with ha 0:
https://toolset.com/errata/checkboxes-that-save-0-are-interpreted-as-checked-by-views/
That is the reason the ordering does not work.

I'd suggest using a field there like a number or single line, or any other field type that you can sort by and gets recognized wit proper values from Views (so for example a radio or a single line or even select field should work)

#1350923

As mentioned before, this field was not defined to save 0, it's something i updated as a way to see if it will help with the toolset sort. Why does it not work even when not set to save a 0?

#1350929

Also, if you change the sorting to be by post date, you will see that it's returning a lot more products . I am not clear why are you saying it's working correctly, when clearly it is not.

#1350935

Yes, I understand. But it makes no difference.

1. If you do not save 0, it will mean the fields will not have any value for the field when it's not checked. WordPress Core WP_Query cannot be filtered or ordered by values that do not exist. So all posts where you select nothing for that checkbox would not appear in the view if ordering or querying by that field.

2. Hence you'd save 0, but if you save 0 https://toolset.com/errata/checkboxes-that-save-0-are-interpreted-as-checked-by-views/ happens, and hence, the verdict is that you cannot use Checkbox field like that to order/sort by.

You should create or use another field to have that kind of ordering, and always add a secondary order by to catch the ones with the same value

You can use a single line field a select field, even a radio should work (but keep it simple, the best is to use a select field I think for your purpose, or a single line - but select is better, as the value cannot be altered while saving the post).

There is no solution to have a Checkbox Field work like this, given above 2 points. Views will assume them checked in any case, whether checked or not - if it saves 0
And if it does not, the View cannot order by as the WP Query does not allow this

#1351153

Hi,
I have created a new field called new new-product (New Select) as a select field and updated a few products to use this field. However, when i sort based on this field, the sort still doesn't happen and it just limits the products that are returned and the limit is not based on the products that have the value defined in the postmeta table, but on something else ( i am not sure what). Can you please advise.

If you look at what data is returned when ordering is set to by Post Date, you will see all the products that we need to have returned all that is needed is the sorting to be adjusted based on the new (or old) custom field.

#1352187

Any suggestions?

#1352341

Yes, I notice these are the same 2 views as the other ticket and these views can not work the way they are set up.

I elaborated in the other ticket about it.

I will analyze this issue here as soon I have a local copy as requested on the other ticket, if possible to provide.
(If the issue persists after fixing the views and what I outline below)

1. Right now, on your site, I see it is ordered by New product Field and returns 4 items 3 of which have the New badge, but actually none of them, has YES for the new (product) field. They all have NO - so there is nothing it can sort by.

2. You have set the values of those fields to be numbers again as 1 and 0.
There is a 0 default saved so all the products where you did not save that field will have a 0 value. But you order as a string. That cannot work.

3. Additionally you order by the old field, not the new.
Your new field is "New". Not "New Product" as selected right now in the view.
You can see your last created field here.
hidden link
The field is New with slug new-product and hence will appear as "New" in the View, not as "New Product", that is your old field, the checkbox:
New Product/ new_product

You should order by the new field.

Thanks.

#1352349

New Products is the newly created field, it's using select rather than checkbox. There are 3 products using this field, 2 have Yes and 1 No.

Please close this ticket, I don't think we understand each other. I will open a new ticket and would like a different support agent to assist. thank you!

#1352353
Bildschirmfoto 2019-10-01 um 15.44.35.png
Bildschirmfoto 2019-10-01 um 15.44.15.png

I am sure the new field is "New" and not new product.
Please find it here and in the screenshot.
hidden link

I cannot change the fact that this is the new field you created and is a select field:
New

There is the old field, a checkbox:
New Product

Feel free to close and/or request another supporter eye on this, maybe I confuse the fields.

#1352451
2019-10-01 06_04_18-database-sort.png
2019-10-01 06_05_30-Front-end-order.png
2019-10-01 06_07_08-View-Ordering.png
2019-10-01 06_09_58-custom-field.png

Yes, I would like this ticket to be transferred to another support person.

Attached are screenshots of all the configurations and a database result which i expect to match on the front end.

Thanks

#1352495

Exactly as I outlined, the field is the "NEW" Field, but in the View, you used the "NEW PRODUCT" Field.
This was before you changed it, and provided the new screenshots.

Now you order that View by the "NEW" Field and by number, which is correct.

On here, hidden link now 4 different products appear.
They have each different values for the NEW field (1 time yes, 3 times no).

We should either get Metal Leg Nesting Tables first, and all other last, or those others first and Metal Leg Nesting Tables last, because that is the one with "yes" and the others are with "no" value.

If I alter the sorting in the view to descending I expect the current order on the page inverted.
But that does not work, and now we can also not alter anymore the Secondary ordering in that view, due to a new JS error that appeared, please find it in the console of your preferred browser.
It says "Uncaught TypeError: Cannot read property 'fields_and_views' of undefined".
It happens in hidden link

This issue is once again not replicable, locally, I am sure it is either a specific issue due to some settings that we could figure out by changing settings in the view, or maybe a 3rd party or other code.

The error did not happen before, hence it should be simple to narrow down the issue causing it (the last action or change done in this site).
I can help finding this cause on a duplicate, or test site, because it will involve also testing with no other code or plugins.

I will now assign this ticket to a colleague, as you requested.

Thank you.

#1352531
2019-10-01 06_58_47-Change-country.png
2019-10-01 06_58_47-Change-country.png

For the new support person , please change the country on the top right of the site to be United States to be looking at the same data.

I a not see the js error mentioned above (but it could be because of the country selection Beda had), screenshot attached.