Skip Navigation

[Resolved] Sorting not working for string or numeric fields?

This support ticket is created 4 years, 1 month 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 25 replies, has 4 voices.

Last updated by Luo Yang 4 years ago.

Assisted by: Luo Yang.

Author
Posts
#1542251

I am trying to: Sort the page by various fields

Link to a page where the issue can be seen:

hidden link

wordpress login (user): toolsetuser
pass: (I had to remove this as I realized it was publishing this - How can I send the pass to you?)

I expected to see:
Using the sort dropdowns - Sort by Municipality (a custom field w/ strings in it) is not working properly.
Sort by Address for some reason is working properly.
Sort by Property Size is working properly.
Sort by MinDiv is not working.

Side question:
To edit these sort fields - Can I just edit the shortcodes that are generated in the page? (Will that update the database / or whatever backend logic is happening) or do I have to recreate the entire sort section by hand, each time I make a sort change? (Just asking as I have 25ish sort fields for 2 different post types, it could take hours to recreate it each time i have a sort change).

Appreciate any help / thoughts here...

Here is the code that generates the sort dropdown currently:

[wpv-sort-orderby type="select" options="post_date,post_title,field-Municipality,field-Major_Use,field-Class,field-subuse,field-Class,field-Property_Size,field-Min_Divisble,field-Max_Contiguous,field-Avail_SF,field-Space_Desc,field-Floor_Number,field-Sublease,field-Lease_Price,field-Lease_Type,field-Sale_Price,field-Owner,field-Listing_No" label_for_post_date="Revised Date" label_for_post_title="Address" label_for_field-Municipality="Municipality" label_for_field-Major_Use="Major Use" label_for_field-subuse="Sub Use" label_for_field-Class="Class" label_for_field-Property_Size="Property Size" label_for_field-Min_Divisble="Min Divisible" label_for_field-Max_Contiguous="Max_Contiguous" label_for_field-Avail_SF="Avail SF" label_for_field-Space_Desc="Space Desc" label_for_field-Floor_Number="Floor Number" label_for_field-Sublease="Sublease" label_for_field-Lease_Price="Lease Price" label_for_field-Lease_Type="Lease Type" label_for_field-Sale_Price="Sale_Price" label_for_field-Owner="Owner" label_for_field-Listing_No="Listing No" orderby_as_numeric_for="field-Property_Size" orderby_ascending_for="post_title,field-Municipality,field-Major_Use,field-Class,field-Listing_No" orderby_descending_for="post_date,field-Property_Size"][wpv-sort-order type="select" options="desc,asc" label_for_asc="Ascending" label_for_desc="Descending" label_asc_for_post_date="Older first" label_desc_for_post_date="Newer first" label_asc_for_post_title="A to Z" label_desc_for_post_title="Z to A" label_asc_for_field-Municipality="Ascending" label_desc_for_field-Municipality="Descending" label_asc_for_field-Major_Use="Ascending" label_desc_for_field-Major_Use="Descending" label_asc_for_field-Class="Ascending" label_desc_for_field-Class="Descending" label_asc_for_field-Property_Size="Ascending" label_desc_for_field-Property_Size="Descending" label_asc_for_field-Listing_No="Ascending" label_desc_for_field-Listing_No="Descending"][wpv-filter-submit name="Sort" class="green-button"]

If you need, your user is a wordpress admin so feel free to edit that view if needed,
Thank you all for any help,
T

#1542343

All,
Just a note - I am splitting out my last question into it's own ticket, so this ticket is just for fixing the sort dropdowns (I'm unable to edit my request above now, but remove the "Side question" from this ticket.

#1542429

Team,
I'm baffled on this. I'm editing Properties post type here:
hidden link

And I've removed the entire sort functionality.

If I add a sort for Zip_Code as a numeric search, with asc or desc options, that works fine.

But as soon as I add one more sort option (eg sort by Address as a string), the zip code sort breaks.

I cannot for the life of me figure out how to fix this,
Please help,
T

#1542433

Here's the sort (in desc order) working for zip_code (as a number) when zip_code is the only sort option:

hidden link

Works fine both asc and desc.

Now I will add a string field to sort on (Municipality) and the Municipality string field is not sorting properly:
hidden link

Now I remove them both.

Now If I add Municipality by itself, it works fine:
hidden link

But now if I add Zip_Code now, that doesn't sort properly:
hidden link

What do I do here? Any ideas?

Thank you all again,
Ti

#1542973

I've enabled a private reply as I can't see the site in question.

Related to why this breaks, I am not sure, I need to see what you set up and how.
Please provide me with the links to the View, the page, the content and eventual templates used.

Thanks

#1544419

Beda or anyone,

Is anyone able to help me on this? As far as I can tell sorting is not working. Anyone please help, I have to present to the client tomorrow and right now this is a big issue and I don't even have an idea on how to fix it...

Please help

#1544599

Team,
Is there anyone that can help me? I'm willing to pay. As of right now I can't get the sort to work. It seems to work for the first sort field I enter, but if I add a second field, it will not work for the second sort field.

Please help all, anyone,
T

#1544745

Team,

Ok so I've spent 16 hours on this one issue, lots of fun definitely.

So not happy about that but I think I've found a viable solution (not the best and it's definitely a hack but unless someone responds here this is the only way I can get sorting to work.

I found this no joke I've searched upwards of 40 tickets in my 16 hours of troubleshooting here, and this is working for me (however it requires me to define every single field in functions as well as in the view interface (which makes future maintenance also: lots of fun) but for now, this is working:

in functions.php

/* 
  toolset sort 
*/
add_filter('wpv_filter_query', 'add_custom_fields_to_view_query', 199, 3);
function add_custom_fields_to_view_query($query_args, $view_settings, $view_id ) {
  var_dump($query_args);
  
  if(isset($query_args['meta_key']) && $query_args['meta_key']=='Municipality'){
    $query_args['orderby'] = 'meta_value';
  }
  if(isset($query_args['meta_key']) && $query_args['meta_key']=='Zip_Code'){
    $query_args['orderby'] = 'meta_value_num';
  }
  
  return $query_args;
}

For the next poor sap trying to fix this issue: In the above code, the zip_code field is my numeric custom field (non-toolset custom field) that I'm sorting, and the Municipality is my non-toolset string custom field that I'm sorting. Feel free to add more fields, but for every field, you have to add them all here.

Beda (or if anyone from toolset does read this) - Can you tell me if this is the best option? Or if what I've done here will break in any subsequent upgrades?

Appreciate any help,
T

#1545227

It shouldn't be needed, to start with.

Unfortunately, I was on my weekend, so I had no time to check your site.
I will do so today and update you with what I find!

#1545491

I think you have some var_dump left on the page:

array(12) { ["post_type"]=> array(1) { [0]=> string(7) "listing" } ["paged"]=> string(1) "1" ["suppress_filters"]=> bool(false) ["ignore_sticky_posts"]=> bool(true) ["posts_per_page"]=> int(20) ["wpv_original_limit"]=> int(-1) ["wpv_original_offset"]=> int(0) ["wpv_original_posts_per_page"]=> int(20) ["post_status"]=> array(2) { [0]=> string(7) "publish" [1]=> string(7) "private" } ["meta_key"]=> string(13) "Property_Size" ["orderby"]=> string(14) "meta_value_num" ["order"]=> string(4) "DESC" }

Related to the issue itself, I see this is a View, used on an Archive.
This setup is unusual, usually, you have an Archive on an Archive.

I'm also not sure how you insert this View on the archive. Via PHP render View API?

All this could be related to the issue, additionally to other plugins or the Theme, because when I try to order a View on test sites, by numeric and string, it works fine.
I added a screenshot of my sorting settings, they should be similar to yours.
On my test I can sort and order by number or string, on the front end, asc and desc.

I can't find a ZIP Code on your sorting, thou, which is the numeric value?

Generally, the sort options on that View should work without any need for Custom Code.

#1545699

Beda,
Ok thank you - I will look into everything you've sent,
T

#1546517

If you want, I can take a look at a local copy, or testing (staging) instance.

We'd need to make sure to strip down the actual "issue" and make sure nothing else interferes.

I have enabled a private reply in case you would like to share a copy of the site, or have a staging instance where we can freely change things?
See also https://toolset.com/faq/provide-supporters-copy-site/

Thank you!

#1547249

Beda - I appreciate it, I'm working on moving it out of the archive page to another page to see if that fixes it now. For now - can you give me a few days to work on it? (Keep this ticket open while I work if that's ok?). Appreciate your help, I'll let you know how it goes,
T

#1547789

Unfortunately, I can't keep tickets open, that's administrated by a robot.
Somewhere in about 3 days after this message, if you were not to reply an email would be sent to remind you the last time to reply here, as otherwise, it'll close the thread.

You might have seen those emails in previous threads too. What you can do at that point is simply comment here very shortly that you aren't yet ready, and make sure, when you submit your reply to NOT mark the ticket as "my issue is resolved" or any "fixed" state, instead, make sure to mark it as "I still need assistance" or "My issue is not resolved yet"

I will then get a ping and will have to reply, and the process will start again until the next reminder 🙂

I hope this is OK?

#1556097

Beda - I'm still working on this - I think your note that it shouldn't be on an archive page might be the problem, I will let you know shortly sorry for the wait

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