在嵌套的WP查询中使用ACF值查找具有过去日期值的CPT

时间:2021-06-23 作者:mtm

我使用一对ACF日期字段来创建对自定义帖子类型(事件)的查询。我有一个;“过去的事件”;feed忽略了我创建以下行为的常用技巧:

如果事件(CPT)有结束日期值,请检查它是否在过去,否则如果事件没有结束日期值,请使用事件日期值(必填字段)并检查它是否在过去,我已多次将这两个子句嵌套在OR关系中,但在这种情况下,这不起作用。任何过去具有event\\u date值的事件都会出现在“past Events”查询结果中,即使现在和将来都有end\\u date值。

这是完整的查询–它与我通常的方法不同,因为它需要过滤掉更多选项:event\\u type(单选按钮)和include\\u in\\u past\\u events(true/false)。

date_default_timezone_set(\'Europe/London\'); 
$past = date(\'Ymd\', strtotime("-999 months")); 
$now = date(\'Ymd\', strtotime("now")); 

$past_args = array(
 \'post_type\' => \'event\',
 \'meta_query\' => array(
  \'relation\' => \'AND\',
   array(
    \'relation\' => \'AND\',
    array(
     \'relation\' => \'OR\',
     // check to see if end date has been set
     array(
      \'key\' => \'end_date\',
      \'compare\' => \'BETWEEN\',
      \'type\' => \'NUMERIC\',
      \'value\' => array($past, $now),
     ),
     // if no end date has been set use event/start date
     array(
      \'key\' => \'event_date\',
      \'compare\' => \'BETWEEN\',
      \'type\' => \'NUMERIC\',
      \'value\' => array($past, $now),
     )
    ),
    array(
     \'key\' => \'event_type\',
     \'value\' => array(\'Singlar\', \'Multi-Day\', \'Series\'),
    )
   ),
   array(
    \'key\' => \'include_in_past_events\',
    \'value\' => \'1\',
   )
  ),
  \'meta_key\'    => \'event_date\',
  \'orderby\' => \'meta_value_num\',
  \'order\' => \'DESC\',
  \'nopaging\'    => false,
  \'posts_per_page\'=> \'8\'
 );
我不明白为什么这对我不起作用,如果有任何见解,我将不胜感激。

谢谢

1 个回复
最合适的回答,由SO网友:bosco 整理而成

您当前的元查询体现了逻辑

IF event_end IN range 
OR event_start IN range
您需要考虑event_end\'s单独存在:

IF event_end IN range
OR ( event_end NOT EXISTS AND event_start IN range )
具有相同和/或的紧邻嵌套子句relation 可以组合使用,这可能会使内容更具可读性。

a AND ( b AND ( c OR d ) ) === a AND b AND ( c OR d )
a OR ( b OR ( c AND d ) ) === a OR b OR ( c AND d )
您还可以name your clauses 为了对使用的结果进行排序,但我还没有对此进行测试。

在实践中,上述所有内容:

$past_args = array(
  \'post_type\'  => \'event\',
  \'meta_query\' => array(
    \'relation\' => \'AND\',
    array(
      \'relation\'   => \'OR\',
      // check to see if end date has been set and is in range
      \'end_clause\' => array(
        \'key\'     => \'end_date\',
        \'compare\' => \'BETWEEN\',
        \'type\'    => \'NUMERIC\',
        \'value\'   => array($past, $now),
      ),
      // if no end date has been set use event/start date
      array(
        \'relation\' => \'AND\',
        array(
          \'key\'     => \'end_date\',
          \'compare\' => \'NOT EXISTS\',
        ),
        \'start_clause\' => array(
          \'key\'     => \'event_date\',
          \'compare\' => \'BETWEEN\',
          \'type\'    => \'NUMERIC\',
          \'value\'   => array($past, $now),
        ),
      ),
    ),
    array(
      \'key\'   => \'event_type\',
      \'value\' => array(\'Singlar\', \'Multi-Day\', \'Series\'),
    ),
    array(
      \'key\'   => \'include_in_past_events\',
      \'value\' => \'1\',
    )
  ),
  // \'meta_key\'      => \'event_date\',// not needed when ordering by clause name.
  \'orderby\'       => \'end_clause start_clause\',
  \'order\'         => \'DESC\',
  \'nopaging\'      => false,
  \'posts_per_page\'=> \'8\',
);

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post