使用今天的ACF日期>查询自定义发布类型 [英] Query custom post type with ACF Date > today

查看:0
本文介绍了使用今天的ACF日期>查询自定义发布类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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);
}

但是,我什么也得不到。

有什么建议我做错了吗?

感谢您的回复!

推荐答案

$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 );

试试这个

这篇关于使用今天的ACF日期>查询自定义发布类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆