我在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。
答案 0 :(得分:0)
问题以这种方式解决了:
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 );
if( !empty( $_GET['country'] ) ) {
$query->set( 'meta_query', $metaquery1 );
} else {
$query->set( 'meta_query', $metaquery2 );
}
}
}