您可以尝试替换这种比较:
AND m1.meta_value >= \'$start_date\'
使用:
AND CAST(m1.meta_value AS DATE) >= \'" . date( "Y-m-d H:i:s", $start_date ) . "\'
或者,如果要对元数据使用Unix时间戳,则可以尝试:
AND m1.meta_value+0 >= ". strtotime( $start_date ) ."
更新:以下是对您的查询的修改:
$query = " SELECT DISTINCT * FROM {$wpdb->posts}
INNER JOIN {$wpdb->postmeta} m1 ON ( {$wpdb->posts}.ID = m1.post_id )
INNER JOIN {$wpdb->postmeta} m2 ON ( {$wpdb->posts}.ID = m2.post_id )
WHERE {$wpdb->posts}.post_type = \'tribe_events\'
AND {$wpdb->posts}.post_status = \'publish\'
AND (
( ( m1.meta_key = \'_EventStartDate\' AND CAST( m1.meta_value AS DATE ) >= %s ) AND ( m2.meta_key = \'_EventStartDate\' AND CAST( m2.meta_value AS DATE ) <= %s ))
OR
( ( m1.meta_key = \'_EventEndDate\' AND CAST( m1.meta_value AS DATE ) >= %s ) AND ( m2.meta_key = \'_EventEndDate\' AND CAST( m2.meta_value AS DATE ) <= %s ) )
OR
( ( m1.meta_key = \'_EventStartDate\' AND CAST( m1.meta_value AS DATE ) < %s ) AND ( m2.meta_key = \'_EventEndDate\' AND CAST( m2.meta_value AS DATE ) > %s ) )
)
ORDER BY {$wpdb->posts}.post_date DESC;";
$query = $wpdb->prepare( $query, $start_date, $end_date, $start_date, $end_date, $start_date, $end_date );
$result = $wpdb->get_results( $query );
这两个
INNER JOINS
已添加,因为您正在使用
AND
在两个不同的元键上。