Skip Navigation

[Résolu] Sort view by numeric custom field value

Ce fil est résolu. Voici une description du problème et la solution proposée.

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 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.

Marqué : 

This topic contains 2 réponses, has 2 voix.

Last updated by Daniel Il y a 5 années et 11 mois.

Assigned support staff: Minesh.

Auteur
Publications
#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: Anglais (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!