Skip Navigation

[Resolved] Sort view by numeric field dont work

This support ticket is created 5 years, 4 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
- 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/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by poulP 5 years, 4 months ago.

Assisted by: Waqar.

Author
Posts
#1338365

I have a custom number field on a post form, where users can input a price. I have set up a validation so that only values from 1 til 999, 1.000 til 999.999 are accepted. So I use the dot as a thousand separator.

I want to have a view/page that shows all ads sorted by the custom price field ascending , and another view/page with
descending sort.

hidden link (ordered by Field - wpcf-pris-for-kunstvaerk, ascending )
hidden link (ordered by Field - wpcf-pris-for-kunstvaerk, descending )

I have used your guide here: https://toolset.com/forums/topic/sort-view-by-numeric-custom-field-value/#post-370481

And have added following code to my functions.php:

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'] = 'pris-for-kunstvaerk'; 
    $query_args['meta_type'] = 'numeric'; //
return $query_args;
}

However, as you can see on the page links above the sorting dont work. kr. 950 is in the page shown as a higher price than 5.700 kr. and 8.000 kr.

Can you tell me how to make it work?
Thanks,
Poul

#1338513

Hi Poul,

Thank you for waiting.

You can create a custom shortcode ( ref: https://toolset.com/documentation/adding-custom-code/how-to-create-a-custom-shortcode/ ) which can get a numerical value and then show it in the desired format, through the "number_format" function.
( ref: hidden link )

Example:


add_shortcode("show_formatted_number", "show_formatted_number_func");
function show_formatted_number_func($atts) {

	$a = shortcode_atts( array(
		'number' => ''
	), $atts );

	if( (!empty($a['number'])) && (is_numeric($a['number'])) ) {
		$number = number_format($a['number'], 2, ',', '.');
	}

	return $number;
}

The above code can be added into the active theme's "functions.php" file.

Next, you can use this new shortcode, inside your page/view's content like this:


[show_formatted_number number='[types field="price-field-slug"][/types]']

Please replace "price-field-slug" with the actual slug of your field.

I hope this helps and let me know if you need any further assistance around this.

Note: For more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1338631

Hi,
thanks. It works...but I need a number format without decimals.
With the above shortcode I get 8.000,00
But I want 8.000 (without decimals).
Can you tell me what to changhe in the shortcode to get rid of the (,) decimals?
Best,
Poul

#1338643

Hi Poul,

Thanks for the update and glad that it works.

If you'd like to format the number, without any decimal places, please update line# 10 in the code, from:


$number = number_format($a['number'], 2, ',', '.');

To:


$number = number_format($a['number'], 0, ',', '.');

regards,
Waqar

#1338645

My issue is resolved now. Thank you!