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 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程