简体   繁体   English

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

[英]wordpress Wp_query and meta query issue with date field

I have Projects inserted as posts in my WordPress database. 我在我的WordPress数据库中将项目作为帖子插入。 currently on my home, the last 3 published project is displayed. 目前在我家,显示最后3个已发布的项目。 now my purpose is that I want first display the project which is expiring today than the last published project. 现在我的目的是首先要显示今天到期的项目,而不是上次发布的项目。

for example, there are 2 projects are expiring today than on the home page it will display 2 projects which are expiring today and 1 project which published last. 例如,有2个项目今天到期,而不是在主页上它将显示2个今天到期的项目和1个最后发布的项目。 it means a total of 3 projects will display. 这意味着总共会展出3个项目。

please check below WP_query which returns last published project only 请检查下面的WP_query,它仅返回上次发布的项目

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

the below query I try using meta key & value but no luck. 以下查询我尝试使用元键和值,但没有运气。 "ign_fund_end" is stored a date as a string so I think that's why not comparing date. “ign_fund_end”将日期存储为字符串,因此我认为这就是为什么不比较日期。 my final goal is I described as above total 3 projects should display. 我的最终目标是上面描述的总共3个项目应该显示。 first should be today expiring then after last published. 首先应该是今天到期,然后在上次发表之后。

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

please check the below image for reference. 请查看下面的图片以供参考。 在此输入图像描述

any solution appreciated. 任何解决方案赞赏

You just need to remove type from the array parameters. 您只需要从数组参数中删除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
            )
    ));

To: 至:

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

Note: The reason is that in table meta_value is not DATE type. 注意:原因是表中meta_value不是DATE类型。

In the PHPMyAdmin, the default date type is: 在PHPMyAdmin中,默认日期类型为:

2019-04-16

Since your custom field ign_fund_end is not in MySQL date compatible format so that is the main reason for your WP_Query to not work in the expected way. 由于您的自定义字段ign_fund_end不是MySQL日期兼容格式,因此这是WP_Query无法以预期方式工作的主要原因。 My recommendation is to save a timestamp of end date in a custom field using save_post and then change the $args for WP_Query to work on that field. 我的建议是使用保存结束日期的时间戳在自定义字段save_post ,然后更改$argsWP_Query对这一领域的工作。

Here is complete solution for your issue: 以下是您的问题的完整解决方案:

1: Save Timestampe in a Custom field 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: Modify the $args 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'
    )
));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM