net*_*dev 5 php wordpress woocommerce divi
我正在打电话,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'] );
}
Run Code Online (Sandbox Code Playgroud)
是否存在任何已知的对 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;
}
Run Code Online (Sandbox Code Playgroud)
或者主题的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;
Run Code Online (Sandbox Code Playgroud)
小智 0
我没有 Divi 来测试它,但我知道主题有自己的查询设置。
尝试我找到的这个解决方案
add_action( 'woocommerce_product_query', 'my_pre_get_posts_query' );
function my_pre_get_posts_query( $q ) {
if ( $q->is_main_query() && ( $q->get( 'wc_query' ) === 'product_query' ) ) {
$query->set( 'post__in', ['245374','245372'] );
}
}
Run Code Online (Sandbox Code Playgroud)