【问题标题】:Query custom post type with ACF Date > today使用 ACF 日期 > 今天查询自定义帖子类型
【发布时间】:2019-11-16 16:52:23
【问题描述】:

我正在使用PHP 7.3.5and wordpress 5.2.x

我有以下自定义帖子类型:

register_post_type('Calendar-Events', array(
    'supports' => array('title', 'editor', 'thumbnail', 'custom-fields', 'excerpt', 'comments', 'revisions'),
    'public' => false,
    'show_ui' => true,
    'exclude_from_search' => true,
    'menu_position' => 5,
    'labels' => array(
        'name' => 'Calendar Events',
        'add_new_item' => 'Add New Calendar Event',
        'edit_item' => 'Edit Calendar Event',
        'all_items' => 'All Calendar Events',
        'singular_name' => 'Calendar Event',
    ),
    'menu_icon' => 'dashicons-calendar-alt',
));

我的帖子类型有一个名为ce_timestamp 的自定义字段,它是Date Time Picker,是使用ACF Version 5.8.7 创建的。

我正在尝试查询ce_timestamp 字段晚于今天的所有帖子。

我尝试了以下方法:

$today = date('d/m/Y');

try {
    $args = array(
        'post_type' => 'Calendar-Events',
        'posts_per_page' => -1,
        'post_status' => 'publish',
        'meta_query' => array(
            array(
                'key' => 'ce_timestamp',
                'value' => $today,
                'type' => 'DATE',
                'compare' => '>='
            )
        ),
        'meta_key' => 'ce_timestamp',
    );

    $upcomingEvents = new WP_Query($args);

    if ( $upcomingEvents->have_posts() ) {

        while ( $upcomingEvents->have_posts() ) {

            $upcomingEvents->the_post();

            // now $query->post is WP_Post Object, use:
            // $query->post->ID, $query->post->post_title, etc.

        }
    }

    wp_reset_postdata();

} catch (\Exception $ex) {
    error_log($ex);
}

但是,我什么也没得到。

任何建议我做错了什么?

感谢您的回复!

【问题讨论】:

    标签: wordpress advanced-custom-fields


    【解决方案1】:
    $today= date('Ymd');
    $args = array(
        'post_type' => 'Calendar-Events',
        'post_status' => 'publish',
        'posts_per_page' => -1,
            'meta_query' => array(
                array(
                    'key' => 'ce_timestamp',
                    'compare' => '>=',
                    'value' => $today,
                ),
            ),
    
        );
    
    $upcomingEvents= new WP_Query( $args );
    

    试试这个

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-27
      • 1970-01-01
      • 2018-10-02
      • 2019-08-28
      • 1970-01-01
      • 1970-01-01
      • 2019-11-30
      • 1970-01-01
      相关资源
      最近更新 更多