【发布时间】:2022-01-06 05:14:14
【问题描述】:
我打电话给woocommerce_product_query,但不在Divi theme 工作。
我检查了一下,它适用于其他内置的 wordpress 主题。
我正在使用Query Monitor,我可以看到它正确触发,但我仍然得到所有产品,而不是我在post__in 中硬编码的产品。
这是我的代码:
add_action( 'woocommerce_product_query', 'my_pre_get_posts_query', 9999 );
function my_pre_get_posts_query( $query ) {
$meta_query = $query->get( 'meta_query' );
$query->set( 'post__in', ['245374','245372'] );
}
Divi 是否存在任何已知的干扰?什么可能出错?我检查了父子 Divi 主题。
Divi 主题代码:
/**
* Filter the products query arguments.
*
* @since 4.0.5
*
* @param array $query_args Query array.
*
* @return array
*/
public function filter_products_query( $query_args ) {
if ( is_search() ) {
$query_args['s'] = get_search_query();
}
if ( function_exists( 'WC' ) ) {
$query_args['meta_query'] = WC()->query->get_meta_query( et_()->array_get( $query_args, 'meta_query', array() ), true );
$query_args['tax_query'] = WC()->query->get_tax_query( et_()->array_get( $query_args, 'tax_query', array() ), true );
// Add fake cache-busting argument as the filtering is actually done in self::apply_woo_widget_filters().
$query_args['nocache'] = microtime( true );
}
return $query_args;
}
或者主题的get_shop公共函数中的这部分:
global $wp_the_query;
$query_backup = $wp_the_query;
$is_offset_valid = absint( $offset_number ) > 0;
if ( $is_offset_valid ) {
self::$offset = $offset_number;
add_filter(
'woocommerce_shortcode_products_query',
// phpcs:ignore WordPress.Arrays.CommaAfterArrayItem.NoComma -- This is a function call.
array( 'ET_Builder_Module_Shop', 'append_offset' )
);
add_filter(
'woocommerce_shortcode_products_query_results',
array( 'ET_Builder_Module_Shop', 'adjust_offset_pagination' )
);
}
if ( 'product_category' === $type || $use_current_loop ) {
add_filter( 'woocommerce_shortcode_products_query', array( $this, 'filter_products_query' ) );
add_action( 'pre_get_posts', array( $this, 'apply_woo_widget_filters' ), 10 );
}
if ( $use_current_loop ) {
add_filter( 'woocommerce_shortcode_products_query', array( $this, 'filter_vendors_products_query' ) );
}
$shop = do_shortcode( $shortcode );
if ( $is_offset_valid ) {
remove_filter(
'woocommerce_shortcode_products_query',
array( 'ET_Builder_Module_Shop', 'append_offset' )
);
remove_filter(
'woocommerce_shortcode_products_query_results',
array( 'ET_Builder_Module_Shop', 'adjust_offset_pagination' )
);
self::$offset = 0;
}
remove_filter( 'woocommerce_default_catalog_orderby', array( $this, 'set_default_orderby' ) );
if ( $use_current_loop ) {
remove_filter( 'woocommerce_shortcode_products_query', array( $this, 'filter_vendors_products_query' ) );
}
if ( 'product_category' === $type || $use_current_loop ) {
remove_action( 'pre_get_posts', array( $this, 'apply_woo_widget_filters' ), 10 );
remove_filter( 'woocommerce_shortcode_products_query', array( $this, 'filter_products_query' ) );
}
$wp_the_query = $query_backup;
【问题讨论】:
-
我刚刚在 Divi 主题中进行了测试,对我来说工作正常。您使用的是哪个版本。
-
@Bhautik 你试过 woocommerce_product_query 了吗?
-
是的,我用过
woocommerce_product_query
标签: php wordpress woocommerce divi