📄 原始资料 | 展开查看原始 JSON(bricks-query_filters-index_post-wcField.json){ “name”: “bricks/query_filters/index_post/wcField”, “kind”: “filter”, “url”: “https://academy.bricksbuilder.io/developer/hooks/filters/bricks-query_filters-index_post-wcfield/”, “scraped_at”: “2026-08-29”, “hook_name”: “b…
📄 原始资料 | 展开查看原始 JSON(bricks-query_filters-index_post-wcField.json)
{
"name": "bricks/query_filters/index_post/wcField",
"kind": "filter",
"url": "https://academy.bricksbuilder.io/developer/hooks/filters/bricks-query_filters-index_post-wcfield/",
"scraped_at": "2026-08-29",
"hook_name": "bricks/query_filters/index_post/wcField",
"description": {
"en": "A specialized hook for indexing WooCommerce product fields (like price, stock status, rating) when a product is saved. This allows the Query Filters index to stay updated with WooCommerce product data.",
"zh": "用于在保存产品时索引 WooCommerce 产品字段(如价格、库存状态、评分)的专门钩子。允许 Query Filters 索引随 WooCommerce 产品数据保持更新。"
},
"parameters": [
{
"name": "rows",
"type": "array",
"type_zh": null,
"description": {
"en": "Array of index rows to be inserted.",
"zh": "将被插入的索引行数组。"
}
},
{
"name": "post_id",
"type": "int",
"type_zh": null,
"description": {
"en": "The ID of the product (post) being indexed.",
"zh": "正在被索引的产品(文章)ID。"
}
},
{
"name": "elements",
"type": "array",
"type_zh": null,
"description": {
"en": "Array of filter elements targeting this product.",
"zh": "以该产品为目标的筛选元素数组。"
}
}
],
"example_usage": "add_filter( 'bricks/query_filters/index_post/wcField', function( $rows, $post_id, $elements ) {\n // This filter is typically used internally by Bricks to handle WooCommerce fields.\n // However, you could hook into it to index custom WooCommerce product data.\n\n\n // Example: Index a custom product property\n $product = wc_get_product( $post_id );\n if ( $product ) {\n // ... generate index rows ...\n }\n\n\n return $rows;\n}, 10, 3 );"
}
前言
用于在保存产品时索引 WooCommerce 产品字段(如价格、库存状态、评分)的专门钩子。允许 Query Filters 索引随 WooCommerce 产品数据保持更新。
用法
将代码加入子主题的 functions.php:
add_filter( 'bricks/query_filters/index_post/wcField', function( $rows, $post_id, $elements ) {
// This filter is typically used internally by Bricks to handle WooCommerce fields.
// However, you could hook into it to index custom WooCommerce product data.
// Example: Index a custom product property
$product = wc_get_product( $post_id );
if ( $product ) {
// ... generate index rows ...
}
return $rows;
}, 10, 3 );
参数
$rows (array):将被插入的索引行数组。
$post_id (int):正在被索引的产品(文章)ID。
$elements (array):以该产品为目标的筛选元素数组。
示例代码
筛选返回值
add_filter( 'bricks/query_filters/index_post/wcField', function( $rows, $post_id, $elements ) {
// This filter is typically used internally by Bricks to handle WooCommerce fields.
// However, you could hook into it to index custom WooCommerce product data.
// Example: Index a custom product property
$product = wc_get_product( $post_id );
if ( $product ) {
// ... generate index rows ...
}
return $rows;
}, 10, 3 );
根据条件短路返回原值
根据条件跳过或修改返回值:
add_filter( 'bricks/query_filters/index_post/wcField', function( $value, $rows ) {
if ( is_admin() ) { return $value; }
// TODO: custom logic here
return $value;
} );
补充说明
更多细节可参考 官方文档。