Bricks 中文文档:bricks/query_filters/index_user/meta_exists

📄 原始资料  |  展开查看原始 JSON(bricks-query_filters-index_user-meta_exists.json){ “name”: “bricks/query_filters/index_user/meta_exists”, “kind”: “filter”, “url”: “https://academy.bricksbuilder.io/developer/hooks/filters/bricks-query_filters-index_user-meta_exists/”, “scraped_at”: “2026-08-29”, “ho…

📄 原始资料  |  展开查看原始 JSON(bricks-query_filters-index_user-meta_exists.json)
{
  "name": "bricks/query_filters/index_user/meta_exists",
  "kind": "filter",
  "url": "https://academy.bricksbuilder.io/developer/hooks/filters/bricks-query_filters-index_user-meta_exists/",
  "scraped_at": "2026-08-29",
  "hook_name": "bricks/query_filters/index_user/meta_exists",
  "description": {
    "en": "Determines if a custom field (meta key) exists for a specific user during the indexing process. This is used when a third-party provider (like ACF or Meta Box) is selected, to avoid indexing empty or non-existent fields for users.",
    "zh": "在索引过程中确定特定用户是否存在自定义字段(meta key)。当选择了第三方 provider(如 ACF 或 Meta Box)时使用,以避免索引用户时空或不存在的字段。"
  },
  "parameters": [
    {
      "name": "exists",
      "type": "bool",
      "type_zh": null,
      "description": {
        "en": "Whether the meta key exists. Default is false .",
        "zh": "meta key 是否存在。默认为 false。"
      }
    },
    {
      "name": "user_id",
      "type": "int",
      "type_zh": null,
      "description": {
        "en": "The ID of the user being checked.",
        "zh": "正在被检查的用户 ID。"
      }
    },
    {
      "name": "meta_key",
      "type": "string",
      "type_zh": null,
      "description": {
        "en": "The meta key of the custom field.",
        "zh": "自定义字段的 meta key。"
      }
    },
    {
      "name": "provider",
      "type": "string",
      "type_zh": null,
      "description": {
        "en": "The data provider (e.g., acf , metabox ).",
        "zh": "数据 provider(例如 acf、metabox)。"
      }
    }
  ],
  "example_usage": "add_filter( 'bricks/query_filters/index_user/meta_exists', function( $exists, $user_id, $meta_key, $provider ) {\n    // Example: Custom check for a serialized meta field on a user\n    if ( $meta_key === 'user_custom_data' ) {\n        $data = get_user_meta( $user_id, $meta_key, true );\n        return ! empty( $data );\n    }\n\n\n    return $exists;\n}, 10, 4 );"
}

前言

在索引过程中确定特定用户是否存在自定义字段(meta key)。当选择了第三方 provider(如 ACF 或 Meta Box)时使用,以避免索引用户时空或不存在的字段。

用法

将代码加入子主题的 functions.php

add_filter( 'bricks/query_filters/index_user/meta_exists', function( $exists, $user_id, $meta_key, $provider ) {
    // Example: Custom check for a serialized meta field on a user
    if ( $meta_key === 'user_custom_data' ) {
        $data = get_user_meta( $user_id, $meta_key, true );
        return ! empty( $data );
    }


    return $exists;
}, 10, 4 );

参数

  • $exists (bool):meta key 是否存在。默认为 false。
  • $user_id (int):正在被检查的用户 ID。
  • $meta_key (string):自定义字段的 meta key。
  • $provider (string):数据 provider(例如 acf、metabox)。

示例代码

筛选返回值

add_filter( 'bricks/query_filters/index_user/meta_exists', function( $exists, $user_id, $meta_key, $provider ) {
    // Example: Custom check for a serialized meta field on a user
    if ( $meta_key === 'user_custom_data' ) {
        $data = get_user_meta( $user_id, $meta_key, true );
        return ! empty( $data );
    }


    return $exists;
}, 10, 4 );

根据条件短路返回原值

根据条件跳过或修改返回值:

add_filter( 'bricks/query_filters/index_user/meta_exists', function( $value, $exists ) {
    if ( is_admin() ) { return $value; }
    // TODO: custom logic here
    return $value;
} );

补充说明

更多细节可参考 官方文档