wordpress Wp_query 和日期字段的元查询问题

wordpress Wp_query and meta query issue with date field

我将项目作为帖子插入到我的 WordPress 数据库中。目前在我的家里,显示了最后 3 个发布的项目。现在我的目的是我想首先显示今天到期的项目而不是上次发布的项目。

例如,今天有 2 个项目到期,那么主页上将显示 2 个今天到期的项目和 1 个最后发布的项目。这意味着总共将显示 3 个项目。

请检查下面 WP_query 哪个 returns 最后发布的项目

$args = array('post_type' => 'ignition_product', 'posts_per_page' => $project_count, 'paged' => $paged);

$newargs = apply_filters('project_query', $args);
$wp_query = new WP_Query($newargs);

下面的查询我尝试使用元键和值但没有成功。 "ign_fund_end" 将日期存储为字符串,所以我认为这就是不比较日期的原因。 我的最终目标是上面描述的总共 3 个项目应该显示。首先应该是今天到期,然后是最后发布之后。

$args = array(
        'post_type' => 'ignition_product',
        'posts_per_page' => $project_count,
        'paged' => $paged,        
        'meta_query' => array(// WordPress has all the results, now, return only the events after today's date
            array(
                'key' => 'ign_fund_end', // Check the start date field
                'value' => date('m/d/Y'), // Set today's date (note the similar format)
                'compare' => '>=', // Return the ones greater than today's date
                'type' => 'DATE' // Let WordPress know we're working with date
            )
    ));

请查看下图以供参考。

感谢任何解决方案。

您只需要从数组参数中删除 type

$args = array(
        'post_type' => 'ignition_product',
        'posts_per_page' => $project_count,
        'paged' => $paged,        
        'meta_query' => array(// WordPress has all the results, now, return only the events after today's date
            array(
                'key' => 'ign_fund_end', // Check the start date field
                'value' => date('m/d/Y'), // Set today's date (note the similar format)
                'compare' => '>=', // Return the ones greater than today's date
                'type' => 'DATE' // Let WordPress know we're working with date
            )
    ));

收件人:

$args = array(
        'post_type' => 'ignition_product',
        'posts_per_page' => $project_count,
        'paged' => $paged,        
        'meta_query' => array(// WordPress has all the results, now, return only the events after today's date
            array(
                'key' => 'ign_fund_end', // Check the start date field
                'value' => date('m/d/Y'), // Set today's date (note the similar format)
                'compare' => '>=', // Return the ones greater than today's date
                //'type' => 'DATE' // Let WordPress know we're working with date
            )
    ));

注:原因是table中的meta_value不是DATE类型。

在 PHPMyAdmin 中,默认的日期类型是:

2019-04-16

由于您的自定义字段 ign_fund_end 不是 MySQL 日期兼容格式,因此这是您的 WP_Query 无法按预期方式工作的主要原因。我的建议是使用 save_post 在自定义字段中保存结束日期的时间戳,然后将 $args 更改为 WP_Query 以在该字段上工作。

这是针对您的问题的完整解决方案:

1:在自定义字段中保存时间戳

add_action( 'save_post', function( $post_id ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    if ( $parent_id = wp_is_post_revision( $post_id ) ) {
        $post_id = $parent_id;
    }

    if( isset( $_POST['ign_fund_end'] ) && !empty($_POST['ign_fund_end']) ) {
        $end_date = $_POST['ign_fund_end'];
        $end_date = strtotime($end_date);
        update_post_meta( $post_id, '__end_date', $end_date );
    }
} );

2:修改$args

$args = array(
    'post_type' => 'ignition_product',
    'posts_per_page' => $project_count,
    'orderby'   => 'meta_value_num',
    'order'     => 'ASC',
    'meta_key'  => '__end_date',
    'paged' => $paged,        
    'meta_query' => array(// WordPress has all the results, now, return only the events after today's date
    array(
        'key' => '__end_date', // Check the start date field
        'value' => strtotime('today'), // Set today's timestamp
        'compare' => '>=', // Return the ones greater than today's date
        'type' => 'NUMERIC'
    )
));