Wpquery order by?

Hey Team, I have a Looper Provider set to Query String. I’d like to order the results by an ACF field (project_number) but I can’t quite figure out what syntax to use. The current query is:

post_type=finished-project&facetwp=true&posts_per_page=25

I’ve tried orderby (and order_by) = project_number but without success.

Hi Gregg,

Thanks for reaching out.

You need to set the orderby to meta_value and specify the meta_key with the specific field name. You can check the following threads, which explain how to use orderby with custom meta values:


Hope this helps.
Thanks!

1 Like

Hey @tristup, Thanks - that worked. Mostly. The project numbers are actual numbers. When I set the order to ASC it doesn’t quite work as expected. We have 4 and 5 digit project numbers - example: 7169 and 13129. I was hoping these would be ordered as 7169 followed by 13129 but they are the opposite - 13129 then 7169. It’s like it’s ready them as text rather than numbers. Is there a way to fix this?

Hi Gregg,

This happens because ACF stores all field values as strings , so numeric values can sometimes be sorted alphabetically instead of numerically. To fix this, make sure your project number field in ACF is set to the Number field type.

Additionally, if you are sorting the values using a query, use:

'orderby' => 'meta_value_num'

This forces WordPress to treat the value as a number when sorting.

Hope this helps!
Thanks.

1 Like

@tristup, excellent. Thank you again. That’s the kind of insight and answer I needed. Much appreciated.

You are most welcome, @lorax

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.