【发布时间】:2017-09-30 12:17:36
【问题描述】:
我在 Wordpress functions.php 中有下一个代码,它在使用表单对帖子进行排序时更改 main_query。 问题是:当$country变量为空时,如何从main_query中排除meta_query的第二个数组?
if ( $query->is_main_query() && ( $orderby || $order || $country ) ) {
if ( in_array( $orderby, array( 'event_start_date' ) ) ) {
$query->set( 'orderby', 'meta_value' );
$query->set( 'order', $order );
$query->set( 'meta_query', array( // WordPress has all the results, now, return only the events after today's date
array(
'key' => 'event_start_date', // Check the start date field
'value' => date_i18n("Y-m-d"), // Set today's date (note the similar format)
'compare' => '>=', // Return the ones greater than or equal to today's date
'type' => 'DATE' // Let WordPress know we're working with date
),
array(
'key' => 'venue_country',
'value' => $country,
)
) );
}
}
更新
我找到了一个解决这个问题的建议here。
【问题讨论】: