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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵