Home › Forums › Front-end Issues › Strange results when using orderby a number field
I have an ACF number field that represents the revenue of a business added to a custom post type called listing.
On the index page for the posts I want default sorting to show the businesses from highest revenue to lowest.
I followed the instructions on this document page – http://www.advancedcustomfields.com/resources/orde-posts-by-custom-fields/
However instead of listing the values in order by size if orders them sequentially.
eg instead of “1, 2, 19, 60, 100, 9999” it lists them as “1, 19, 100, 2, 60, 9999”
<?php
$mypost = array(
‘post_type’ => ‘listing’,
‘posts_per_page’ => 20,
‘meta_key’ => ‘revenue_number’,
‘orderby’ => ‘meta_value_num’,
‘order’ => ‘DESC’
); $loop = new WP_Query( $mypost ); ?>
The only thing I can figure is that when I print out the numbers I dynamically add commas (for clarity) and maybe that is screwing it up?
$<?php echo number_format( floatval( get_field(‘revenue_number’) ), 0, ‘.’, ‘,’ ); ?>
I found this solution previous on https://wordpress.org/support/topic/add-comma-on-number-field
You can see this weirdness in action here –
http://americanhealthcarecapital.com/new-listings/
Thoughts?
Just want to make sure that this is exactly what your code looks like:
<?php
mypost = array(
‘post_type’ => ‘listing’,
‘posts_per_page’ => 20,
‘meta_key’ => ‘revenue_number’,
‘orderby’ => ‘meta_value_num’,
‘order’ => ‘DESC’
); $loop = new WP_Query( $mypost ); ?>
If it is then you’re missing a $
in front of mypost
on the first line.
Sorry John that’s a typo, I fixed it. It’s correct in the template code.
(The loop wouldn’t give any values if it was missing)
Anyway, it seems that the echo number_format adjustment is what confuses the sorter so that it’s treated like a regular string and not a number/currency which is problematic. The commas need to be parsed in before not after. Arg.
So I might have missed something, are you storing it in the database with the commas? If you’re not changing the way the value is stored then that shouldn’t have any effect on the query.
Yes, It’s going into the db table as a plain number. 1000000
I want to display it with commas. 1,000,000
So then that should not be causing the problem of them not being sorted properly. The code you have is correct.
<?php
$mypost = array(
'post_type' => 'listing',
'posts_per_page' => 20,
'meta_key' => 'revenue_number',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
$loop = new WP_Query($mypost);
?>
What is after this? What does you loop look like?
The loop is fine.
The problem is that the jQuery table I’m using (was using, Im trying a different script now) to SORT the data is confusing the parsed numbers with commas as regular strings, not as numbers/values so it orders as 1,100, 1000,2, 20, 2222, 6 instead of 1,2,6,20,100,1000,2222
The problem seems to be that converting the string with <?php echo number_format( floatval( get_field(‘revenue_number’) ), 0, ‘.’, ‘,’ ); ?> confuses the sorter.
If I don’t convert it and just use the regular the_field call, it sorts properly.
I don’t know if there’s a way to call the conversion in the loop prior to the while/loop or if there’s a different way to convert the output
Sounds like you need to find a way to have the jQuery sorter convert the values back to plain numbers before attempting to do the sort.
The topic ‘Strange results when using orderby a number field’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.