Home › Forums › General Issues › Order Posts by Custom Fields When Meta Key Already In Use
I’m trying to order WP_Query posts by an ACF Custom Field.
I’ve been looking at the link below…
https://www.advancedcustomfields.com/resources/orde-posts-by-custom-fields/
Based on that information, I’d be looking at something like this…
$args = array(
'numberposts' => -1,
'post_type' => 'cars',
'meta_key' => 'price',
'orderby' => 'meta_value',
'order' => 'DESC'
'posts_per_page' => 12,
'paged' => $paged
);
My issue is that I’m already using the Meta Key to filter out Sold Cars.
$args = array(
'numberposts' => -1,
'post_type' => 'cars',
'meta_key' => 'sold',
'meta_value' => '0',
'orderby' => $orderBy,
'order' => $order,
'posts_per_page' => 12,
'paged' => $paged
);
Would anyone be able to point me in the right direction as to how I could combine the two?
You need to use a meta_query
instead of meta_key
and meta_value
See this https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters and also see the last code example in the order & orderby parameters section https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters about ordering using meta query clauses.
Many thanks for your reply… I will look into this asap and let you know how I get on. It might be a job for Wednesday now though. đ
Thanks again for your help…
I can’t say I had a good understanding of the information on that page, but I’ve managed to solve my problem.
$args = array(
'numberposts' => -1,
'post_type' => 'cars',
'meta_query' => array(
'state_clause' => array(
'key' => 'sold',
'value' => '0'
)
),
'meta_key' => 'price',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => 12,
'paged' => $paged
);
As you can see, I’ve used the meta_query to filter out the sold vehicles, allowing me to use the meta_key to order the results.
I’ve got a bit of a glitch going on though…
Any prices starting with 1 are at the beginning and any starting with 9 are at the end etc.
This has resulted in ÂŁ30,000 cars being next to ÂŁ300,000 cars.
I’ve checked the type for the Price field and it’s “number”.
Am I missing something simple or should I have done the ordering via meta_query as well?
The post below got me through the number sorting problem…
https://support.advancedcustomfields.com/forums/topic/orderby-custom-field-not-working/
You must be logged in to reply to this topic.
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!
ACF wouldnât be so widely used in WordPress if it didnât have some pretty amazing capabilities. In this article, we look at a few of the features weâll discuss during â7 things you didnât know you could do with ACFâ at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.