如何解决日期时间字段为空或早于 1 天的 Wordpress ACF 查询帖子
我有一个带有价格字段的自定义帖子类型 (affiliate_products
),我每天通过 API 和 WP Cron 作业更新该字段。因为我的网站上有很多附属产品,所以我将它们分成 5 个批次,每分钟运行一个 cron。
为此,我使用了 ACF 日期时间字段 last_update
,该字段会在更新时使用当前日期和时间进行更新。但是我的 WP 查询有问题;
我需要获得 5 个 last_update
字段为空的帖子(尚未更新的新产品)或上次更新超过 1 天前的帖子。 (日期
但是我的查询不起作用。元查询的第一部分正在工作,但是一旦我添加了 meta_query 的第二部分,就不再返回任何结果,即使仍有大量产品的 last_update 字段为空。我的代码可能有什么问题?
我意识到我还没有任何 last_update
日期大于 1 天的产品,但它应该仍然返回带有空 last_update
字段的产品,对吗?
$args_products = array(
'posts_per_page' => 5,'post_type' => 'affiliate_products','meta_query' => array(
'relation' => 'OR',array(
'key' => 'last_update','compare' => 'NOT EXISTS'
),'value' => date('Y-m-d H:i:s'),'compare' => '<','type' => 'DATETIME'
),)
);
$the_product_query = new WP_Query( $args_products );
if( $the_product_query->have_posts() ) {
while( $the_product_query->have_posts() ) {
$the_product_query->the_post();
// the update code goes here
}
}
解决方法
试试 Ymd
格式。在这里查看date-picker
$args_products = array(
'posts_per_page' => 5,'post_type' => 'affiliate_products','meta_query' => array(
'relation' => 'OR',array(
'key' => 'last_update','compare' => 'NOT EXISTS'
),'value' => date('Y-m-d H:i:s',strtotime('-1 day')),'compare' => '<','type' => 'DATETIME'
),)
);
$the_product_query = new WP_Query( $args_products );
if( $the_product_query->have_posts() ) {
while( $the_product_query->have_posts() ) {
$the_product_query->the_post();
// the update code goes here
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。