You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress中visibility=>visible不生效,WC_Product_Query返回空数组求助

Troubleshooting Empty Results from WC_Product_Query with 'visibility' Parameter

Hey there! Let's figure out why your WC_Product_Query is returning an empty array when you're trying to fetch visible products. Here are practical steps to diagnose and fix the issue:

1. Confirm Your Products Are Actually Marked as Visible

First things first: double-check the catalog visibility setting for the products you expect to see. In each product's edit screen, look at the Catalog visibility dropdown (in the Publish metabox). It needs to be set to either:

  • Shop and search results
  • Shop only

If it's set to "Hidden" or "Search only", those products won't be included when filtering for "visible" products via the query.

2. Fix the visibility Parameter Syntax

Wait a minute — the visibility parameter in WC_Product_Query doesn't accept a single string like 'visible'. Instead, it expects an array of the specific visibility types you want to include.

WooCommerce defines "visible" products as those available in the catalog (shop pages) and/or search results. So the correct parameter value is an array containing 'catalog' and 'search'. Here's your corrected code:

$query = new WC_Product_Query( array(
    'visibility' => array( 'catalog', 'search' )
) );
$products = $query->get_products();
print_r($products);

This is the most likely fix for your empty array issue.

3. Check for Conflicting Code or Plugins

Custom plugins, themes, or hooks might be modifying the product query behind the scenes. Try these steps to isolate the problem:

  • Temporarily deactivate all non-WooCommerce plugins.
  • Switch to a default WooCommerce theme like Storefront.
  • Run your query again.

If products now appear, reactivate plugins/themes one by one to find which one is interfering. Common culprits are hooks like woocommerce_product_query or woocommerce_product_query_meta_query that alter the query parameters.

4. Ensure Your Code Runs in the Right Context

If you're executing the code too early (before WooCommerce is fully loaded), the WC_Product_Query class might not be initialized, or product data might not be accessible. Wrap your code in an action that fires after WooCommerce is ready, like woocommerce_init:

add_action( 'woocommerce_init', 'fetch_visible_products' );
function fetch_visible_products() {
    $query = new WC_Product_Query( array(
        'visibility' => array( 'catalog', 'search' )
    ) );
    $products = $query->get_products();
    print_r($products);
}

This ensures your code runs at the proper time in the WordPress load sequence.

5. Debug the Underlying SQL Query

To see exactly what's being sent to the database, add this filter to print the raw SQL query:

add_filter( 'posts_request', 'debug_product_query_sql' );
function debug_product_query_sql( $sql ) {
    echo '<pre>' . esc_html( $sql ) . '</pre>';
    return $sql;
}

Run your code, then copy the SQL and execute it directly in your database (via phpMyAdmin or a similar tool). If the SQL returns no results, you'll know the issue is with how the query is constructed; if it does return results, the problem might be in how WooCommerce is processing the results into product objects.

6. Verify Product Statuses

By default, WC_Product_Query only returns products with a publish status. If your products are draft, pending review, or private, they won't show up. If you need to include other statuses, you can add the status parameter:

$query = new WC_Product_Query( array(
    'visibility' => array( 'catalog', 'search' ),
    'status' => array( 'publish', 'draft' ) // Include draft products if needed
) );

But for standard visible products, make sure your products are set to "Published".


内容的提问来源于stack exchange,提问作者Dimitris Laios

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:26:40