WooCommerce 主商店页面未显示任何产品

发布于 2025-01-10 11:02:07 字数 1107 浏览 0 评论 0原文

我有这个奇怪的问题,每隔一段时间就会发生一次。 (我知道这很有帮助,但让我解释一下)。

我正在构建一个 WooCommerce 网站,每隔一段时间,我就会注意到主商店页面(产品存档页面)不会显示任何产品 - 但是一旦我单击其中一个类别,它正确显示了与该类别相关的所有产品。

奇怪的是,它似乎是随机发生的。为了尝试找到代码中的问题,我删除了一部分,直到它开始工作。然后当我添加回我的代码时,它继续工作并且我没有任何问题 - 这使得这是一个非常奇怪的错误,非常难以诊断,更不用说修复了。

至少我设法回显了实际的 SQL 查询。因此,这里是它在不显示产品时使用的 SQL 查询:

SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND (wp_posts.ID = '18') AND wp_posts.post_type = 'page' ORDER BY wp_posts.post_date DESC

ID 18 的页面是设置为商店页面的实际页面:

在此处输入图像描述

仅供参考,这就是 SQL 查询应该看起来像:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND ( wp_posts.ID NOT IN ( SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id IN (15) ) ) AND ((wp_posts.post_type = 'product' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private'))) GROUP BY wp_posts.ID ORDER BY wp_posts.menu_order ASC, wp_posts.post_title ASC LIMIT 0, 16

那么,这是怎么回事?为什么循环不重置以显示实际产品?

I have this strange problem that occur every once in a while. (Helpful, I know, but let me explain).

I'm building a WooCommerce site and every once in a while, I'm noticing that the main shop page (the product archive page as it were) doesn't show any of the products - but once I click one of the categories, it correctly shows all products connected to that category.

The weird part is that it seems to happens randomly. To try and find the problem in my code, I remove part after part until it starts working. And then when I add back my code, it continues to work and I have no problem whatsoever - making this a very strange bug that's super hard to diagnoze, let alone fix.

At least I managed to echo the actual SQL query. So here is the SQL query that it uses when it shows no products:

SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND (wp_posts.ID = '18') AND wp_posts.post_type = 'page' ORDER BY wp_posts.post_date DESC

The page with the ID 18 is the actual page that is set as shop page:

enter image description here

Just for reference, this is what the SQL query should look like:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND ( wp_posts.ID NOT IN ( SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id IN (15) ) ) AND ((wp_posts.post_type = 'product' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private'))) GROUP BY wp_posts.ID ORDER BY wp_posts.menu_order ASC, wp_posts.post_title ASC LIMIT 0, 16

So, what's going on here? Why doesn't the loop reset to show the actual products?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

调妓 2025-01-17 11:02:07

请从存档页面中删除所有自定义查询并将此代码放入 function.php 文件中。

function fwp_archive_per_page( $query ) {
    if ( is_tax( 'product_cat' ) || is_post_type_archive('product') ) {
        
        $query->set( 'showposts', 16 );
        $query->set( 'posts_per_page', 16 );
        

    }
}
add_filter( 'pre_get_posts', 'fwp_archive_per_page' );

Please Remove any custom query from archive page and put this code in function.php file.

function fwp_archive_per_page( $query ) {
    if ( is_tax( 'product_cat' ) || is_post_type_archive('product') ) {
        
        $query->set( 'showposts', 16 );
        $query->set( 'posts_per_page', 16 );
        

    }
}
add_filter( 'pre_get_posts', 'fwp_archive_per_page' );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文