douji6667 2019-04-01 10:45
浏览 95
已采纳

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

I have Projects inserted as posts in my WordPress database. currently on my home, the last 3 published project is displayed. 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. it means a total of 3 projects will display.

please check below WP_query which returns last published project only

$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. my final goal is I described as above total 3 projects should display. 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. enter image description here

any solution appreciated.

  • 写回答

2条回答 默认 最新

  • duanjiao6711 2019-04-01 12:28
    关注

    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. 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.

    Here is complete solution for your issue:

    1: Save Timestampe in a Custom field

    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

    $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'
        )
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波