Skip Navigation

[Resolved] “as number” sort setting ignored in filtered results

This thread is resolved. Here is a description of the problem and solution.

Problem:
"as number" sort setting ignored in filtered results

Solution:
You should use view's API filter hook "wpv_filter_query" to override the view's query on fly.

You can find the proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/as-number-sort-setting-ignored-in-filtered-results/#post-1234141

Relevant Documentation:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 5 years 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 Minesh 4 years, 3 months ago.

Assisted by: Minesh.

Author
Posts
#1231555
Screen Shot 2019-04-16 at 9.10.44 AM.png

I am trying to: Show a list of properties sorted by their property numbers as numbers not as alphabetical sorting (so 2 comes before 17, for example). This works fine in the main view but I also have a filter where the user can filter the list of properties based on activities available. In the filtered results list, the order still uses the property number but it shows in alphabetic order, not numeric order (9 comes after 10, 2 comes after 17, etc.)

Link to a page where the issue can be seen:
hidden link

I expected to see:
The list always sorted by property number, numerically, whether the filter is applied or not.

Instead, I got:
The list is sorted numerically at first but when the filter is applied, it still filters on the correct field but alphabetically instead of numerically.

Screenshot shows sort setting is property number, ascending, as a number.

#1231592

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - can you please share access details so I can review your current setup and check whats going wrong there.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1233839

Minesh
Supporter

Languages: English (English )

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

Well - I've changed your field type to the numeric field for the field "Property Map Number" which was set to "Single Line" field before.
=> hidden link

I can see now, the listing is sorted now as expected.
=> hidden link

Can you please confirm it works at your end as well.

#1233843

Is there no way to treats numbers as numbers but not use a numeric field? This site needs the flexibility to have a property number such as 14a so changing the field to numeric is not the ideal solution. We just need digits (0-9) sorted numerically if they appear at the beginning of the field contents.

#1233844

Minesh
Supporter

Languages: English (English )

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

Well - what you said both are a different thing. Either you can sort by number or text. If you set to text sort, you will get the results what you have had before and if you change it to number, it will work like after I made the change.

#1233847

Then why do I have the option to set it to "As a Number" in the View editor if it is a single line field instead of a number field? And why does it work correctly before I apply a filter?

I'd think all the "As a Number" setting does is add the SORT_NUMERIC parameter to PHP's sort function or meta_value_num in the WordPress query. It shouldn't matter whether the field type is number or single line with either of those. When the filter is applied, whatever was being done correctly in the original sort is lost and the sort reverts to alpha. That seems like a bug, not an issue with field type.

Can you escalate this to development and have them check whether it is supposed to work or not?

#1234141

Minesh
Supporter

Languages: English (English )

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

Indeed, you were right, the orderby argument is not respected with taxonomy ajax filter. Sure. I've escalated this issue to our next level support.

I found workaround for you that you may use it for now if you do not want to change single line field to number.

Please add this to your current theme's functions.php file OR custom code section offered by Toolset:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

add_filter( 'wpv_filter_query', 'func_change_orderby',99,3 );
function func_change_orderby( $query_args,$views_settings, $view_id) {
      
if ($view_id == 2769){
	$query_args['orderby'] = array( 'meta_value_num' => 'ASC');
    $query_args['meta_key'] = 'wpcf-property-map-number'; 
    $query_args['meta_type'] = 'numeric'; 
}
    return $query_args;
}
#1234873

Minesh
Supporter

Languages: English (English )

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

Can you confirm that workaround I shared help you to resolve your issue?

#1235040

We changed the field to a number for now as we don't have any alphanumeric property entries yet (ie. 14a). This was simpler than adding the custom code you provided. We'd like to change the field back to single line when this is officially fixed in a future update. For now, you can close this ticket as long the bug is on the dev team's bug list.

#1235387

Minesh
Supporter

Languages: English (English )

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

Great, I already reported the bug to in front of our Devs.

Please feel free to close this ticket as, as a supporter, we should not close it 🙂

#1235592

Work-around in place for now. Looking forward to the bug being fixed by the development team.

#1448183

Minesh
Supporter

Languages: English (English )

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

I would like to inform you that the latest release of Toolset Views version 3.0.2, includes a fix for this issue. Please feel free to update the Toolset views.

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