最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Query custom post type with ACF Date

programmeradmin2浏览0评论

I am using PHP 7.3.5 and wordpress 5.2.x.

I have the following custom post type:

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',
));

My post type has a custom field that is called ce_timestamp which is a Date Time Picker and was created using ACF Version 5.8.7.

I am trying to query all posts where the ce_timestamp field is later than today.

I tried the following:

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

However, I get nothing back.

Any suggestions what I am doing wrong?

I appreciate your replies!

I am using PHP 7.3.5 and wordpress 5.2.x.

I have the following custom post type:

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',
));

My post type has a custom field that is called ce_timestamp which is a Date Time Picker and was created using ACF Version 5.8.7.

I am trying to query all posts where the ce_timestamp field is later than today.

I tried the following:

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

However, I get nothing back.

Any suggestions what I am doing wrong?

I appreciate your replies!

Share Improve this question asked Nov 16, 2019 at 7:08 Carol.KarCarol.Kar 3771 gold badge8 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Please check the ce_timestamp meta_key row in postmeta table. And make sure you have the valid dateformat as meta value.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论