wordpress Elementor中两个ACF日期字段之间的查询

kgsdhlau  于 2023-06-21  发布在  WordPress
关注(0)|答案(1)|浏览(127)

如果当前日期介于ACF设置的开始日期和结束日期之间(包括开始日期和结束日期),我将尝试查询事件。我的ACF字段的返回值格式也是Ymd。这是我目前所知道的

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' => '>='
            ),       
        ];
 } );

目前,我没有得到任何结果,我能做错什么?

nszi6y05

nszi6y051#

我可以通过将value的日期格式设置为current_time('Ymd')来实现这一点。另外,看起来我忘记了在追加 meta查询后设置查询。

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 );
 } );

相关问题