Skip Navigation

[Resolved] Sort view by numeric custom field value

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

Problem:
How to sort view using numeric field was created by ACF.

Solution:
Using view's "wpv_filter_query" hook you can modify the run time query.

You should add "wpv_filter_query" hook to your current theme's funcitons.php file.
Code Example : https://toolset.com/forums/topic/sort-view-by-numeric-custom-field-value/#post-370481

Where:
change "cf-job-growth-pct" with your orginal field slug.

Relevant Documentation:
https://toolset.com/documentation/user-guides/views-filters/wpv_filter_query/

This support ticket is created 8 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
- 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)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by Daniel 8 years, 2 months ago.

Assisted by: Minesh.

Author
Posts
#370376

I am trying to to sort multiple views output by a numeric custom field.
The numeric field was created by ACF and contains numbers from 0 to 999.

I've read in another post that there is a feature planned for setting a custom field to numeric in the "order by" dialog. But that feature is not ready yet.

How can I sort these views?

Thanks,
Daniel

#370481

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

To filter your custom field in numeric format. Could you please try to add following code to your current theme's function.php file:

add_filter( 'wpv_filter_query', 'hook_search_custom_fields', 10, 2 );
function hook_search_custom_fields( $query_args, $view_settings ) {
      
    $query_args['orderby'] = array( 'meta_value_num');
    $query_args['meta_key'] = 'cf-job-growth-pct'; 
    $query_args['meta_type'] = 'numeric'; //
     
     
return $query_args;
}

Where:
change "cf-job-growth-pct" with your orginal field slug.

I hope this solution with help you to resolve your issue. Please feel free to get in touch with me if you need further assistance.

#370500

Great, that works like a charm.

Thank you!

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