Divi 主题和 woocommerce_product_query 不起作用
Posted
技术标签:
【中文标题】Divi 主题和 woocommerce_product_query 不起作用【英文标题】:Divi Theme and woocommerce_product_query not working 【发布时间】: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
【参考方案1】:
我没有 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'] );
【讨论】:
测试过但没用... 然后我建议向主题开发人员寻求最佳解决方案。就像我说的那样,我没有研究它的主题。也许在主题中搜索 woocommerce_product_query 并查看他们是否对其进行了修改。 我问过,他们覆盖了 woocommerce 主题。改变它并不容易,这就是为什么我在这里问是否有人做过类似的事情 那么也许可以分享一下他们是如何改变它的? 我用我发现的代码更新了我的帖子,我认为这与它有关...【参考方案2】:试试这个,它可能对你有帮助。
<?php
add_action( 'woocommerce_product_query', 'my_pre_get_posts_query' );
function my_pre_get_posts_query( $query )
$query->set( 'meta_query', array( array(
'post__in' => ['245374','245372']
)));
【讨论】:
以上是关于Divi 主题和 woocommerce_product_query 不起作用的主要内容,如果未能解决你的问题,请参考以下文章
php Divi的更好,更快,响应的图像 - 在Divi儿童主题上传。
php Divi的更好,更快,响应的图像 - 在Divi儿童主题上传。