英文:
Query between two ACF Date Fields in Elementor
问题
我正在尝试查询一个事件,如果当前日期在由ACF设置的开始日期和结束日期之间(包括在内)。我的ACF字段的返回值格式也是Ymd。到目前为止我有这些...
add_action( 'elementor/query/now_playing', function( $query ) {
// 设置自定义文章类型
$query->set( 'post_type', [ 'shows' ] );
$meta_query = $query->get( 'meta_query' );
// 如果在此过滤器运行时没有元查询,应将其初始化为空数组。
if ( ! $meta_query ) {
$meta_query = [];
}
// 追加我们的元查询
$meta_query[]=[
'relation' => 'AND',
array(
'key' => 'start_date',
'type' => 'DATE',
'value' => date('Y-m-d'),
'compare' => '<='
),
array(
'key' => 'end_date',
'type' => 'DATE',
'value' => date('Y-m-d'),
'compare' => '>='
),
];
} );
目前,我没有得到任何结果,我可能做错了什么?
英文:
I am trying to query for an event if the current date is between (inclusive) the start date and end date set by ACF. The return value format for my ACF field is Ymd as well. Here's what I have so far...
add_action( 'elementor/query/now_playing', function( $query ) {
// Set the custom post type
$query->set( 'post_type', [ 'shows' ] );
$meta_query = $query->get( 'meta_query' );
// If there is no meta query when this filter runs, it should be initialized as an empty array.
if ( ! $meta_query ) {
$meta_query = [];
}
// Append our meta query
$meta_query[]=[
'relation' => 'AND',
array(
'key' => 'start_date',
'type' => 'DATE',
'value' => date('Y-m-d'),
'compare' => '<='
),
array(
'key' => 'end_date',
'type' => 'DATE',
'value' => date('Y-m-d'),
'compare' => '>='
),
];
} );
Currently, I am not getting any results, what could I be doing wrong?
答案1
得分: 0
我通过将值的日期格式设置为current_time('Ymd')
来使其工作。另外,看起来我在附加元查询之后忘记设置查询了。
add_action('elementor/query/now_playing', function($query) {
$today = current_time('Ymd');
// 设置自定义文章类型
$query->set('post_type', ['shows']);
$meta_query = $query->get('meta_query');
// 如果在此过滤器运行时没有元查询,它应该初始化为空数组。
if (!$meta_query) {
$meta_query = [];
}
// 附加我们的元查询
$meta_query[] = [
array (
'key' => 'start_date',
'value' => $today,
'compare' => '<=',
),
array (
'key' => 'end_date',
'value' => $today,
'compare' => '>=',
)
];
$query->set('meta_query', $meta_query);
});
英文:
I was able to get this working by setting the date format of value to current_time('Ymd')
. Additionally, it looks like I had forgotten to set the query after appending the meta query.
add_action( 'elementor/query/now_playing', function( $query ) {
$today = current_time('Ymd');
// Set the custom post type
$query->set( 'post_type', [ 'shows' ] );
$meta_query = $query->get( 'meta_query' );
// If there is no meta query when this filter runs, it should be initialized as an empty array.
if ( ! $meta_query ) {
$meta_query = [];
}
// Append our meta query
$meta_query[]=[
array (
'key' => 'start_date',
'value' => $today,
'compare' => '<=',
),
array (
'key' => 'end_date',
'value' => $today,
'compare' => '>=',
)
];
$query->set( 'meta_query', $meta_query );
} );
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论