【发布时间】:2018-10-05 22:46:21
【问题描述】:
我正在尝试根据逻辑条件及其自定义字段值在我的网站上隐藏产品(由高级自定义字段插件提供 - ACF)。
我参考了一些关于根据特定标准排除产品的文章,使用 pre_get_posts、woocommerce_product_query_tax_query 和 woocommerce_product_query 挂钩进行测试。所有这些都不允许我访问 ACF 值,以便我可以过滤 $tax_query 术语。
我在这里的最终目标是隐藏在其自定义字段中设置了日期并且该日期是过去的特定产品。我使用get_field('event_date'); 函数访问自定义字段。
我正在寻找帮助我开发此代码以根据产品的高级自定义字段值过滤产品的人。
到目前为止,我已经尝试了很多,我查看了 var_dumps 以了解我可以使用什么,但事实是我仍然处于第一阶段。这是我可笑的进步:
add_action('woocommerce_product_query', 'filter_past_events');
function filter_past_events($q)
{
// Return is event_date variable is not set
if (get_field('event_date')) {
$eventDate = new DateTime(get_field('event_date')); // Format ISO-8601 (YYYY-MM-DD)
$now = new DateTime();
if ($eventDate < $now) {
// event is in the past
}
}
}
到目前为止,我已经参考过:
•Exclude products from a particular category on the shop page
•filter-woocommerce-products-based-on-custom-product-attribute-value
【问题讨论】:
-
您最好使用普通的手工编码自定义字段,而不是使用 ACF,这样您就可以通过元查询访问数据。
-
嗨@LoicTheAztec,我同意你的说法,但是,我使用 ACF 是因为它的便利性和多功能性。我确实找到了这个,虽然这可能对我有帮助...advancedcustomfields.com/resources/query-posts-custom-fields
标签: php wordpress woocommerce