🆕 Bricks 2.4 已发布:查询循环性能提升 40%,迁移教程同步更新 →

首页 / 中文教程 / 教程

如何利用 Meta Box Radio 子字段值筛选 Bricks 文章 Query Loop(教程)

本 Bricks 教程讲解如何按 Meta Box 组的 Radio 类型子字段值筛选文章 Query Loop。通过 PHP query 与序列化字符串匹配,实现按 session 类型(Individual/Duet/Trio)过滤 store 自定义文章类型,适合 WP 与外贸建站开发者。

Ray ChanRay Chan·2026-08-21·约 2 分钟
目录
  1. 1.步骤 1
  2. 2.步骤 2

此前我们曾演示过,如何通过 Meta Box 组的 text/number 与 Datepicker 类型子字段来筛选 Bricks 文章 Query Loop(详见这篇教程)。

本篇 Pro 教程 将讲解当子字段为 Radio 类型时,如何筛选文章。

我们以如下场景为例:

自定义文章类型(CPT):store

字段组结构:

  • session(可克隆组 / cloneable group)
    • session_name(text 文本)
    • session_type(radio 单选),可选值为:
      • Individual
      • Duet
      • Trio

字段组配置截图

一篇文章可以包含 1 个或多个 session:

多 session 文章截图 1

另一篇文章:

多 session 文章截图 2

我们的目标是:筛选出所有 session 类型为指定字符串(即 IndividualDuetTrio)的文章。

步骤 1

在页面(Page)或模板(Template)中设置 Query Loop。

启用 PHP query(PHP 查询)。

粘贴以下代码:

$session_type = 'Individual';

// Validate session type
$valid_types = [ 'Individual', 'Duet', 'Trio' ];
if ( ! in_array( $session_type, $valid_types, true ) ) {
  return [ 'post__in' => [ 0 ] ]; // Return empty result for invalid session type
}

return [
  'post_type'      => 'store',
  'posts_per_page' => -1,
  'no_found_rows'  => true,
  'meta_query' => [ // Meta query to filter by session type
    [
      'key'     => 'session',
      'value'   => '"session__type";s:' . strlen( $session_type ) . ':"' . $session_type . '"',
      'compare' => 'LIKE',
    ],
  ]
];

在上述代码的第一行填入要筛选的 session 类型。

"session__type";s:' . strlen( $session_type ) . ':"' . $session_type . '"

该 pattern 会生成一个搜索字符串,用于匹配 Meta Box 数据中 session__type 字段的 PHP 序列化格式,其中包含了精确的字段名、字符串标识、字符长度以及字段值本身。

对代码进行签名(Sign code)并保存。

步骤 2

在已启用 Query Loop 的区块(Block)内添加一个 Post Title(文章标题)元素。

大功告成!现在你的 Query Loop 就会被筛选,只显示那些至少包含一个分组、且其对应的 Meta Box radio 类型子字段值匹配的文章。

相关阅读

Ray Chan

站长

Ray Chan

WordPress Developer & Bricks Specialist

WordPress developer with 10+ years of client builds. Switched to Bricks in 2023 — now builds fast WordPress sites and migrates legacy Elementor/Divi projects.

延伸阅读