如何优化产品捆绑包的慢慢自定义WP查询?
我试图在第二天对WP查询进行故障排除,我正在疲倦并需要帮助。我设法使它起作用,但是非常慢(需要5秒即可加载而不是约0.5s)。
我的问题:我需要将不可用的产品移至WP查询的末尾,然后通过在“菜单_erder”面板中进行排序对其进行排序。
我正在使用不使用“ _STOCK_STATUS”的产品捆绑包插件,但有其自己的meta_key” _WC_PB_BUNDLED_ITEMS_STOCK_STATUS”,该集合中的1/4填充了“ Instock”和“ Outofstock”值。我不知道为什么100%的产品不是这种情况。当产品变得不可用时,它具有“ _stock_status” = instock,但是另一个“ _wc_pb_bundled_items_stock_status” = Outofstock出现。
我猜这是100%运行顺利的,我可以对“ _wc_pb_bundled_items_stock_status”进行排序。
我的代码:
<?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'product',
'posts_per_page' => 18,
'paged' => $paged,
'product_cat' => 'zestawy',
'meta_query' => array(
'relation' => 'OR',
'instock_default_order' => array(
'relation' => 'OR',
array(
'key' => '_wc_pb_bundled_items_stock_status',
'value' => 'instock',
'compare' => '=',
),
array(
'key' => '_stock_status',
'value' => 'instock',
'compare' => '=',
),
),
'outofstock_order' => array(
array(
'key' => '_wc_pb_bundled_items_stock_status',
'value' => 'outofstock',
'compare' => '=',
),
)
),
'orderby' => array(
'_wc_pb_bundled_items_stock_status' => 'DESC',
'menu_order' => 'ASC',
),
);
$wp_query = new WP_Query( $args );
?>
我开始阅读有关如何优化此代码并使用“ PRE_GET_POSTS”功能的有关的代码。这将页面加载时间从5s减少到4s,但效果仍然很差。经过多次尝试和更改后,我设法编写了此代码:
function instock_bundle_query($query){
if ( is_product_category('zestawy') && $query->is_main_query() ) {
$query->set('meta_query', array(
'instock_default_order' => array(
'relation' => 'OR',
array (
'key' => '_wc_pb_bundled_items_stock_status',
'compare' => 'NOT EXISTS',
),
array (
'key' => '_wc_pb_bundled_items_stock_status',
'compare' => 'EXISTS',
),
)
));
$query->set('orderby',array(
'meta_value' => 'ASC',
'menu_order' => 'ASC'
));
}};
add_action( 'pre_get_posts', 'instock_bundle_query' );
它的工作速度要快得多,但工作不正常。不可用的产品在列表的末尾,但是没有Meta_key的捆绑包” _wc_pb_bundled_items_stock_status”首先显示,其次是具有meta_key“ instock”的捆绑。有什么方法可以改善这一点吗?
I am trying to troubleshoot my WP Query for the second day and I am getting tired and need help. I managed to make it work, but very slow (needs 5s to load instead of about 0.5s).
My problem: I need to move the unavailable products to the end of WP Query, and sort the rest by sorting in the "menu_order" panel.
I'm using the Product Bundles plugin, which doesn't use "_stock_status" but has its own meta_key "_wc_pb_bundled_items_stock_status", which for 1/4 of the sets is populated with "instock" and "outofstock" values. I have no idea why this is not the case with 100% of the products. When a product becomes unavailable, it has "_stock_status" = instock, but an additional "_wc_pb_bundled_items_stock_status" = outofstock appears.
I guess if this worked 100% smoothly, I could just sort the "_wc_pb_bundled_items_stock_status" meta_key ascendingly and additionally use "menu_order" sorting.
My code:
<?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'product',
'posts_per_page' => 18,
'paged' => $paged,
'product_cat' => 'zestawy',
'meta_query' => array(
'relation' => 'OR',
'instock_default_order' => array(
'relation' => 'OR',
array(
'key' => '_wc_pb_bundled_items_stock_status',
'value' => 'instock',
'compare' => '=',
),
array(
'key' => '_stock_status',
'value' => 'instock',
'compare' => '=',
),
),
'outofstock_order' => array(
array(
'key' => '_wc_pb_bundled_items_stock_status',
'value' => 'outofstock',
'compare' => '=',
),
)
),
'orderby' => array(
'_wc_pb_bundled_items_stock_status' => 'DESC',
'menu_order' => 'ASC',
),
);
$wp_query = new WP_Query( $args );
?>
I started reading about how to optimize this code and used the "pre_get_posts" function. This reduced the page load time from 5s to 4s, but the effect is still poor. After many attempts and changes I managed to write this code:
function instock_bundle_query($query){
if ( is_product_category('zestawy') && $query->is_main_query() ) {
$query->set('meta_query', array(
'instock_default_order' => array(
'relation' => 'OR',
array (
'key' => '_wc_pb_bundled_items_stock_status',
'compare' => 'NOT EXISTS',
),
array (
'key' => '_wc_pb_bundled_items_stock_status',
'compare' => 'EXISTS',
),
)
));
$query->set('orderby',array(
'meta_value' => 'ASC',
'menu_order' => 'ASC'
));
}};
add_action( 'pre_get_posts', 'instock_bundle_query' );
Which works much faster, but does not work properly. Unavailable products are at the end of the list, but bundles that do not have meta_key "_wc_pb_bundled_items_stock_status" are displayed first, followed by those that have the meta_key "instock". Is there any way to improve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论