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条)

报告相同问题?

悬赏问题

  • ¥15 如何构建全国统一的物流管理平台?
  • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
  • ¥20 和学习数据的传参方式,选择正确的传参方式有关
  • ¥15 这是网络安全里面的poem code
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)
  • ¥50 python写segy数据出错
  • ¥20 关于线性结构的问题:希望能从头到尾完整地帮我改一下,困扰我很久了
  • ¥30 3D多模态医疗数据集-视觉问答
  • ¥20 设计一个二极管稳压值检测电路