I think the below solution will work-
add_action( 'pre_get_posts', 'the_dramatist_filter_shop_order' );
function the_dramatist_filter_shop_order( $query ) {
if( is_admin() && $query->is_main_query() && in_array( $query->get('post_type'), array('shop_order') ) ) {
add_filter( 'posts_join', 'the_dramatist_shop_order_query_join' );
add_filter( 'posts_where', 'the_dramatist_shop_order_filter_date' );
}
}
function the_dramatist_shop_order_filter_date( $where ){
global $wpdb;
$where .= " AND pm.meta_key = '_delivery_date_ymd' AND pm.meta_value != '' AND pm.meta_value < " . $wpdb->prefix ."posts.post_date";
return $where;
}
function the_dramatist_shop_order_query_join( $join ){
global $wpdb;
$join .= ' RIGHT JOIN ' . $wpdb->prefix . 'postmeta pm ON pm.post_id = ' . $wpdb->prefix . 'posts.ID ';
return $join;
}
// remove_filter('posts_join', 'the_dramatist_shop_order_query_join' ); // remove filter because not to effect another query to use after this query.
// remove_filter('posts_where', 'the_dramatist_shop_order_filter_date' ); // remove filter because not to effect another query to use after this query.
I’ve not found any way to remove this filters after pre_get_posts
query is completed. It’s better and best practice to remove those filters after the query is completed. But also it’s not that necessary here cause we are only running this query on shop_order
post type.
Hope that helps.