【问题标题】:Date issue in custom post type loop (wordpress)自定义帖子类型循环中的日期问题(wordpress)
【发布时间】:2015-08-12 14:09:10
【问题描述】:

我正在使用带有自定义日期字段的自定义帖子类型在页面上显示游览日期。有些帖子有“从”和“到”日期(因为它们持续超过一天),有些只有“从”(因为它们只有一天),我想要所有未来或当前的旅行要显示的日期。

这一切都运行良好,除了某些原因,如果该日期是 2016 年或更晚,只有“开始”日期的帖子会停止显示。这对我来说没有任何意义!

这是参数:

$today = date('Ymd');
$tour_args = array(
    'post_type' => 'tour_date',
    'posts_per_page' => -1,
    'orderby' => 'meta_value',
    'meta_key' => 'tour-date-from',
    'order' => 'ASC',
    'meta_query' => array(
      'relation' => 'OR',
      array(
        'key' => 'tour-date-from',
        'value' => $today,
        'compare' => '<=',
      ),
      array(
        'key' => 'tour-date-to',
        'value' => $today,
        'compare' => '>=',
      ),
    ),
);

【问题讨论】:

  • 你有没有试过摆脱'meta_key' =&gt; 'tour-date-from',,因为你正在做meta_query
  • 感谢 rnevius!我只是做了那个调整,但没有任何改变。我认为它需要meta_key 才能找到meta_value

标签: php wordpress date


【解决方案1】:

现在已经解决了。问题是我需要指定元类型。我还需要取出“meta_key”行,因此修改“orderby”行以寻找正确的值。

$today = date('Ymd');
$tour_args = array(
    'post_type'      => 'tour_date',
    'posts_per_page' => -1,
    'orderby'        => 'tour-date-from',
    'order'          => 'ASC',
    'meta_query'     => array(
      'relation'     => 'OR',
      array(
        'key'     => 'tour-date-to',
        'value'   => $today,
        'compare' => '>=',
        'type'    => 'NUMERIC'
      ),
      array(
        'key'     => 'tour-date-from',
        'value'   => $today,
        'compare' => '<=',
        'type'    => 'NUMERIC'
      ),
    ),
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多