I have custom meta fields, delivery_date
and collection_date
on WooCommerce orders.
I actually have various fields with the same data, in various formats (Y-m-d
; U
; D d/m/Y
) any of which may be useful.
I'd like to list (in an admin page; is_admin() && $query->is_main_query()
) all invalid orders - those where the delivery date or collection date is present and is prior to the postdate
.*
I can't see how to create meta_query
s which compare a postmeta value with a post value, do I need to use a posts_join
and posts_where
?
Apologies if I haven't explained this well.
edit: The following MySQL query fetches the correct posts:
SELECT p.ID, p.post_date, pm.meta_value, pm.meta_key
FROM wp_postmeta pm
LEFT JOIN wp_posts p ON pm.post_id = p.ID
WHERE pm.meta_key = '_delivery_date_ymd'
AND pm.meta_value != ''
AND pm.meta_value < p.post_date
Similar, but unanswered question: How can I create a WP_Query that returns posts where one meta_value <= another meta_value
*- This occurs when WooCommerce Subscriptions create renewal orders and the postmeta is copied verbatim.
shop_order
post type ?shop_order
(and only when I've checked a checkbox I've added to the filters)