两个日期之间的meta_query在wordpress中不起作用

时间:2017-04-10 11:12:56

标签: wordpress

我尝试了 meta_query 但没有工作。

需要在2016-01-01至2017-01-01特定日期之间发布数据,但我的代码无效:

$args = array( 
     'post_type' => 'post',
    'order' => 'ASC',
    'orderby' => 'title',
    'category' => array( 7, 11, 463, 409 ),
    'post_status' => array('publish'),  
    'posts_per_page' => -1,
     'meta_query' => array(
        array(
            'key' => 'post_date',
            'value' => array("2016-01-01", "2017-01-01"),
            'compare' => 'BETWEEN',
            'type' => 'DATE'
        )
    )
    );

2 个答案:

答案 0 :(得分:0)

您需要确保您的meta仅包含日期格式,测试以下代码也使用var_dump转储查询或回显运行的查询取决于您的args运行数据库中的查询以验证数据是否已设置以您希望通过查询获取的格式相同的格式。注释掉类别行以确保您的帖子是否返回,如果返回然后检查帖子,则在帖子中选择了类别

$start = '2016-01-01';
$end   = '2017-01-01';
$args  = array(
    'post_type'      => 'post',
    'posts_per_page' => - 1,
    'order'          => 'ASC',
    'orderby'        => 'title',
    'category'       => array(7, 11, 463, 409),
    'meta_key'   => 'post_date',
    'meta_query' => array(
        array(
            'key'     => 'post_date',
            'value'   => array($start, $end),
            'compare' => 'BETWEEN',
            'type'    => 'DATE'
        )
    )
);
// Make the query
$Query = new WP_Query();
echo 'SQL: '.$Query->request.'<br>'; // your query against args

答案 1 :(得分:0)

我找到了解决方案,请查看以下代码:

    $args = array( 
         'post_type' => 'post',
        'order' => 'ASC',
        'orderby' => 'title',
        'category' => array( 7, 11, 463, 409 ),
        'post_status' => array('publish'),  
        'posts_per_page' => -1,
        'date_query' => array(
            array(
                'column' => 'post_modified',
                'after'     => $last_fetch_date,
                'before'    => $current_time,
                'inclusive' => true,
            ),
        ),
   );