Home › Forums › Front-end Issues › Order products first which have field
Hello, in woo products i have added repeater (pasirinkti_datas) and in repeater are datepicker field (keliones_datos). How i can order products first which have datepicker field? Also i need to order products from nearly date to latest. Also one product can have multiple dates.
This is Example what i need:
+Product (2022-12-24)
+Product (2022-12-26)
+Product (2022-12-29)
+Product (without date)
+Product (without date)
….
I have added this code into functions.php
add_filter('woocommerce_get_catalog_ordering_args', 'woocommerce_catalog_orderby');
function woocommerce_catalog_orderby( $args ) {
$args['meta_key'] = 'pasirinkti_datas_0_keliones_datos';
$args['order'] = 'ASC';
$args['orderby'] = 'meta_value_num';
return $args;
}
But it now only shows posts with dates, posts that don’t have dates disappear.
Also, first repeater row is always the earliest date, so i think my meta_key can be ‘pasirinkti_datas_0_keliones_datos’.
You are trying to do 2 things that are practically not possible is WP.
For the first problem. If you order posts by a meta field that may or may not exist, WP will only return posts where a db row for the post ID and meta key exist. Even if you find a way to populate all of the posts with a value, lets say NULL or an empty string, these will always be first when ordering in ASC order. So, if you want to order dates in ASC order then the ones with empty values will always be first unless they have a value that is greater then all other “dates”.
To solve this you will need to make sure every post has a value in the field and you will need to make sure that the value for posts that don’t have a value will sort in the order that you want them. For example, using a date field, if you want the unset values to be shown last then you could use the value of “Z” to represent an unset value and use the meta type “CHAR” which will sort numbers first followed by letters.
The second problem is that repeater sub fields are stored with multiple different meta keys. It is not possible to do a db query that will treat multiple db rows as being the same. ORDERBY (a|b|c|d) ASC
is not possible. This may be possible by doing a nested query in SQL and setting the orderby on the results of a nested query. Extremely complicated stuff, the following is an extremely simplified example.
SELECT date AS
(SELECT meta_value
FROM wp_postmeta
WHERE meta_key LIKE "repeater_%_sub_field")
ORDERBY date ASC
But anything like this is simply not possible using WP_Query.
To solve this issue you will need to create an additional meta key in the DB that stores values in the standard WP format where each meta_key can have multiple values/rows. https://web.archive.org/web/20190814230622/https://acfextras.com/dont-query-repeaters/
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!
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.