Wordpress meta_query 自定义字段中存储的日期后的某些天数

Wordpress meta_query of certain days after date stored in custom field

我想将自定义字段中存储的日期设置为 meta_query 10 天。

我是这样设置的,但是不行

元键“2a”的值类似于“2022-02-15”。

<?php
$today = wp_date('Y-m-d');


$args = array(
'meta_query' => array(
                      'relation' => 'AND',


array(
    'key'=> '2a',
    'value' => array( date("Y-m-d", strtotime("+10 day", strtotime(get_post_meta($post->ID , '2a' ,true)))),  $today ),
    'compare' => '<=',
    'type' => 'DATE',
    ),

array(
    'key'=> '3a',
    'compare' => 'EXISTS'
    ),


                                                                                             
));?>

您将两个值传递到查询的日期部分,这表明您需要比较 <=

我倾向于发现最好先执行计算,然后将结果传递到查询中。像您的示例中那样将它们合并在一起会使它更难理解。

虽然 strtotime() 会起作用,但 类 可以使处理时间更容易且更易读。

示例:

// Keeping the code brief for the example but you'll probably want to do some checks here.
$twoADate = new DateTimeImmutable( get_post_meta( $post->ID , '2a' ,true ) );

$tenDaysLater = $twoADate->modify( '+10 days' )->format( 'Y-m-d' );
$today = wp_date( 'Y-m-d' );

$args = [
    'meta_query' => [
        'relation' => 'AND',
        [
            'key'=> '2a',
            'value' => [$tenDaysLater, $today],
            'compare' => 'BETWEEN',
            'type' => 'DATE', 
        ],
        [
            'key' => '3a',
            'compare' => 'EXISTS'
        ],
    ],
];
$today = wp_date('Y-m-d');
$TenDaysBefore = date("Y-m-d", strtotime("-10 day", strtotime($today)));

'key'=> '2a',
'value' => $TenDaysBefore ,
'compare' => '<=',
'type' => 'DATE',

现在有效