【问题标题】:Trouble displaying custom posts only if the date field is after today's date仅当日期字段晚于今天的日期时才会显示自定义帖子
【发布时间】:2018-01-30 05:05:13
【问题描述】:

我试图仅在“日期”字段在今天之后的情况下显示自定义帖子类型的帖子。自定义帖子类型为“事件”,“日期”字段格式为 yyyy-mm-dd。 但是,当我使用以下查询时,我的所有事件都会返回,即使其中两个事件早于今天的日期。

           <?php

                $today = date('Y-m-d');
                $args = array (
                    'post_type' => 'Event',
                    'meta_query' => array(
                         array(
                            'key'       => 'Date',
                            'compare'   => '>=',
                            'value'     => $today
                        )
                    ),
                );

                 $query = new WP_Query($args);  
                ?>

如果我使用 date_query 代替如下,则不会返回或显示任何内容。

     <?php $today = date('Y-m-d');

            $args = array (
                'post_type' => 'Event',
                'date_query' => array(
                     array(
                        'key'       => 'Date',
                        'after'     => $today
                    ),
                    'inclusive' => true,
                ),
            );


             $query = new WP_Query($args);  ?>

有人能指出我正确的方向吗?

【问题讨论】:

    标签: php wordpress custom-post-type


    【解决方案1】:

    你可以试试这个,如果不行则需要匹配日期格式

    <?php
    
                    $today = date('Y-m-d');
                    $args = array (
                        'post_type' => 'Event',
                        'meta_query' => array(
                             array(
                                'key'       => 'Date',
                                'compare'   => '>=',
                                'value'     => $today,
                                'type' => 'DATE',
                            ),
                        ),
                    );
    
                     $query = new WP_Query($args);  
                    ?>
    

    【讨论】:

    • 那不起作用,我想我需要匹配日期格式。感谢您为我指明正确的方向
    • 你可以试试这个功能 $originalDate = "2010-03-21"; $newDate = date("d-m-Y", strtotime($originalDate));
    【解决方案2】:

    您应该使用 current_time() 函数来获取今天的日期,它会根据 wordpress 时间格式为您提供时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-27
      • 1970-01-01
      • 2013-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-28
      相关资源
      最近更新 更多