Meta Box in Custom Queries
Most Meta Box fields work automatically in Builderius without writing any GraphQL code. Fields appear in the Dynamic Data dropdown and can be added to any element with a click. Learn about built-in Meta Box integration →
This article provides guidelines for when you need to use Meta Box within your custom queries Pro Feature, or when you need specific control not aviailble out of the box.
When to Use Custom Meta Box GraphQL Queries
Built-in Meta Box integration covers most use cases. Use custom GraphQL queries when you need:
- Advanced filtering - Use Meta Box fields in query arguments
- Complex relationships - Complex Meta Box connections
- Custom formatting - Transform field values with expressions
- Performance control - Optimize expensive queries with
@private - Cross-context access - Meta Box fields from different post types together
Meta Box Field Access Methods
Understanding the difference between metabox_value() and specialized Meta Box methods:
Builderius provides two approaches for accessing Meta Box fields - simple value retrieval and advanced object access. The choice depends on how much control you need over the returned data.
Simple vs Advanced Methods
Simple Method (metabox_value):
Returns the field value as configured in Meta Box settings - could be just an ID, URL, or basic data depending on field configuration.
Advanced Methods (metabox_post_value, metabox_user_value, etc.):
Always return the full WordPress object regardless of Meta Box field settings, giving you complete access to all object properties and related data.
Post Object Example
Simple Method
- GraphQL
- JSON
GraphQL with metabox_value():
{
post {
# Returns whatever the Meta Box field format is set to
featured_article: metabox_value(field_id: "featured_article")
}
}
JSON Output (depends on Meta Box field settings):
{
"featured_article": {
"ID": 123,
"post_author": "5",
"post_date": "2024-11-21 10:30:00",
"post_date_gmt": "2024-11-21 10:30:00",
"post_content": "<!-- wp:paragraph --><p>Full article content here...</p><!-- /wp:paragraph -->",
"post_title": "How to Build Better Websites",
"post_excerpt": "Learn the fundamentals...",
"post_status": "publish",
"comment_status": "open",
"ping_status": "open",
"post_password": "",
"post_name": "how-to-build-better-websites",
"post_modified": "2024-11-21 15:45:00",
"post_modified_gmt": "2024-11-21 15:45:00",
"post_parent": 0,
"guid": "https://site.com/?p=123",
"menu_order": 0,
"post_type": "post",
"post_mime_type": "",
"comment_count": "12"
}
}
Advanced Method
- GraphQL
- JSON
GraphQL with metabox_post_value():
{
post {
# Select only the fields you need
featured_article: metabox_post_value(field_id: "featured_article") {
post_title
permalink
featured_image {
file_url
alt_text
}
# Access Meta Box fields on the related post
reading_time: metabox_value(field_id: "reading_time")
}
}
}
JSON Output (only requested fields):
{
"featured_article": {
"post_title": "How to Build Better Websites",
"permalink": "https://site.com/how-to-build-better-websites/",
"featured_image": {
"file_url": "https://site.com/wp-content/uploads/hero.jpg",
"alt_text": "Website screenshot"
},
"reading_time": "8 minutes"
}
}
The key difference: The advanced method requires you to define subfields and gives you control over which data you retrieve, making queries more efficient by requesting only what you need, rather than getting the entire object dump.
How do you later access these subfields? Use the Dynamic Data dropdown, click the post object field that appears, and select the subfield when the object expands. A [[custom.featured_article.post_title]] will be added to your element.
Meta Box Syntax Rule
Meta Box field access depends on context:
Settings Pages (Root Level) - REQUIRES option_name:
metabox_value(field_id: "contact_email", option_name: "site_settings")
Posts/Pages/Terms/Users - NO option_name:
metabox_value(field_id: "contact_email")
This is the most common source of errors when working with Meta Box in GraphQL.
Querying Meta Box Fields
- Simple Values
- Object Fields
- Array Fields
metabox_value(field_id: "field_name") - Returns the field value
Applies to all basic field types:
- Text, Textarea, Number, Email, URL
- Select, Radio, Checkbox
- Date, Datetime
- Color, Switch (boolean)
- Image/File fields (returns URL or object based on settings)
{
post {
# Text and content fields
subtitle: metabox_value(field_id: "subtitle")
price: metabox_value(field_id: "price")
featured: metabox_value(field_id: "featured")
event_date: metabox_value(field_id: "event_date")
brand_color: metabox_value(field_id: "brand_color")
# Image field (with size parameter)
hero_image_url: metabox_value(field_id: "hero_image", args: "size=medium")
# Transform values
formatted_price: expression_result(
expression: "'$' . price"
)
formatted_date: expression_result(
expression: "date('F j, Y', strtotime(event_date))"
)
}
}
Object fields return single structured data with named properties:
metabox_group_value(field_id: "field_name")- Group fieldsmetabox_post_value(field_id: "field_name")- Single post relationshipmetabox_user_value(field_id: "field_name")- Single user relationshipmetabox_taxonomy_value(field_id: "field_name")- Single term relationship
{
post {
# Group field - organized related data
contact_info: metabox_group_value(field_id: "contact_details") {
phone: metabox_value(field_id: "phone")
email: metabox_value(field_id: "email")
address: metabox_value(field_id: "address")
}
# Single post relationship
featured_article: metabox_post_value(field_id: "featured_article") {
post_title
permalink
# Access Meta Box fields on related post
article_type: metabox_value(field_id: "article_type")
reading_time: metabox_value(field_id: "reading_time")
}
# Single user relationship
author: metabox_user_value(field_id: "guest_author") {
display_name
user_email
# User Meta Box fields
job_title: metabox_value(field_id: "position")
bio: metabox_value(field_id: "bio_extended")
}
# Single taxonomy relationship
primary_category: metabox_taxonomy_value(field_id: "main_category") {
name
slug
# Term Meta Box fields
category_color: metabox_value(field_id: "color")
icon: metabox_value(field_id: "category_icon")
}
# Access object properties
contact_email: expression_result(
expression: "contact_info.email"
)
}
}
Array fields return multiple values or collections:
metabox_groups_value(field_id: "field_name")- Multiple group instancesmetabox_posts_value(field_id: "field_name")- Multiple post relationshipsmetabox_users_value(field_id: "field_name")- Multiple user relationshipsmetabox_taxonomies_value(field_id: "field_name")- Multiple term relationships- Image Gallery fields - Use
metabox_value()(array of image objects)
{
post {
# Multiple groups (like repeater)
team_members: metabox_groups_value(field_id: "team") {
name: metabox_value(field_id: "member_name")
role: metabox_value(field_id: "position")
photo: metabox_value(field_id: "headshot")
# Calculate within group
display_name: expression_result(
expression: "name . ' - ' . role"
)
}
# Multiple post relationships
related_articles: metabox_posts_value(field_id: "related_content") {
post_title
permalink
excerpt: metabox_value(field_id: "custom_excerpt")
}
# Multiple taxonomy terms
skill_areas: metabox_taxonomies_value(field_id: "expertise") {
name
slug
proficiency: metabox_value(field_id: "skill_level")
}
# Image gallery field
gallery: metabox_value(field_id: "project_gallery")
# Process arrays
gallery_count: expression_result(
expression: "count(gallery)"
)
first_image: expression_result(
expression: "gallery[0].url"
)
team_size: expression_result(
expression: "count(team_members)"
)
total_skills: expression_result(
expression: "count(skill_areas)"
)
}
}
Filtering with Meta Box Fields
Use Meta Box fields in query arguments to filter posts, users, and terms based on custom field values.
Meta Query Syntax Reference
- Single Condition
- Multiple Conditions
- Data Types
- Comparison Operators
Basic single condition syntax:
meta_query: {
array: [
{
key: "field_id"
value: "field_value"
compare: "="
}
]
}
Examples:
# Text field
{
key: "status"
value: "published"
compare: "="
}
# Number field
{
key: "rating"
value: 5
compare: "="
type: "NUMERIC"
}
# Boolean field (Meta Box switch)
{
key: "featured"
value: 1
compare: "="
type: "NUMERIC"
}
Combining conditions with AND/OR:
meta_query: {
relation: "AND" # or "OR"
array: [
{
key: "field1"
value: "value1"
compare: "="
}
{
key: "field2"
value: "value2"
compare: "="
}
]
}
Nested conditions:
meta_query: {
relation: "AND"
array: [
{
key: "status"
value: "active"
compare: "="
}
{
relation: "OR"
array: [
{
key: "priority"
value: "high"
compare: "="
}
{
key: "urgent"
value: 1
compare: "="
type: "NUMERIC"
}
]
}
]
}
Data type specifications:
Text fields (default):
{
key: "category"
value: "tutorial"
compare: "="
# No type needed - default is text
}
Numeric fields:
{
key: "price"
value: 100
compare: ">"
type: "NUMERIC"
}
Date fields:
{
key: "event_date"
value: "2024-12-01"
compare: ">="
type: "DATE"
}
Switch fields (Meta Box boolean):
{
key: "featured"
value: 1
compare: "="
type: "NUMERIC"
}
Comparison operators:
Equality:
compare: "=" # Equals
compare: "!=" # Not equals
Numeric comparisons:
compare: ">" # Greater than
compare: ">=" # Greater than or equal
compare: "<" # Less than
compare: "<=" # Less than or equal
Range comparisons:
{
key: "price"
value: [50, 200]
compare: "BETWEEN"
type: "NUMERIC"
}
Text matching:
compare: "LIKE" # Contains text
compare: "NOT LIKE" # Does not contain text
Multiple values:
{
key: "status"
value: ["active", "pending"]
compare: "IN"
}
Field existence:
compare: "EXISTS" # Field has any value
compare: "NOT EXISTS" # Field is empty or missing
Queries With Meta Box Filtering
- Basic Filtering
- Multiple Conditions
- Common Patterns
Simple field value filtering:
{
# Featured articles
featured_articles: posts_query(
arguments: {
post_type: "article"
meta_query: {
array: [
{
key: "featured"
value: 1
compare: "="
type: "NUMERIC"
}
]
}
}
) {
posts {
post_title
featured: metabox_value(field_id: "featured")
}
}
Combining multiple field conditions:
{
# Articles that are both featured AND published
featured_published: posts_query(
arguments: {
post_type: "article"
meta_query: {
relation: "AND"
array: [
{
key: "featured"
value: 1
compare: "="
type: "NUMERIC"
}
{
key: "article_status"
value: "published"
compare: "="
}
]
}
}
) {
posts {
post_title
featured: metabox_value(field_id: "featured")
status: metabox_value(field_id: "article_status")
}
}
Common filtering patterns:
{
# Posts missing required fields (quality check)
incomplete_articles: posts_query(
arguments: {
post_type: "article"
meta_query: {
array: [
{
key: "author_bio"
compare: "NOT EXISTS"
}
]
}
}
) {
posts {
post_title
has_bio: expression_result(
expression: "metabox_value('author_bio') ? 'Yes' : 'No'"
)
}
}
# Order by Meta Box field value
events_by_date: posts_query(
arguments: {
post_type: "event"
orderby: "meta_value"
meta_key: "event_date"
order: "ASC"
}
) {
posts {
post_title
event_date: metabox_value(field_id: "event_date")
}
}
# Filter by relationship field
posts_by_author: posts_query(
arguments: {
post_type: "article"
meta_query: {
array: [
{
key: "guest_author"
value: 123
compare: "="
type: "NUMERIC"
}
]
}
}
) {
posts {
post_title
author: metabox_user_value(field_id: "guest_author") {
display_name
}
}
}
}
Query Meta Box Based on Different Contexts
Meta Box fields must be accessed from within their respective WordPress objects, with special syntax for settings pages.
- Post Context
- Term Context
- User Context
- Navigation Context
- Settings Context
Current Post:
{
post {
post_title
subtitle: metabox_value(field_id: "subtitle")
event_date: metabox_value(field_id: "event_date")
featured: metabox_value(field_id: "featured")
}
}
Specific Post:
{
about_page: post(identifier: "slug", value: "about") {
post_title
company_name: metabox_value(field_id: "company_name")
founding_year: metabox_value(field_id: "founded")
}
}
Multiple Posts:
{
articles: posts_query(
arguments: {
post_type: "article"
posts_per_page: 5
}
) {
posts {
post_title
reading_time: metabox_value(field_id: "reading_time")
author_bio: metabox_value(field_id: "author_bio")
}
}
}
Current Term (Archive Context):
{
archive {
term {
name
description
category_color: metabox_value(field_id: "brand_color")
category_icon: metabox_value(field_id: "icon")
}
}
}
Specific Term:
{
marketing_category: term(
identifier: "slug"
value: "marketing"
taxonomy: "category"
) {
name
description
header_image: metabox_value(field_id: "category_header")
featured_on_homepage: metabox_value(field_id: "featured")
}
}
Multiple Terms:
{
categories: terms_query(
arguments: {
taxonomy: "category"
hide_empty: true
}
) {
terms {
name
slug
category_color: metabox_value(field_id: "brand_color")
commission_rate: metabox_value(field_id: "commission_rate")
}
}
}
Current User:
{
current_user {
display_name
user_email
job_title: metabox_value(field_id: "position")
bio: metabox_value(field_id: "bio_extended")
}
}
Specific User:
{
author_profile: user(identifier: "ID", value: 5) {
display_name
author_bio: metabox_value(field_id: "author_bio")
website: metabox_value(field_id: "personal_website")
}
}
Multiple Users:
{
team_members: users_query(
arguments: {
role: "author"
number: 10
}
) {
users {
display_name
department: metabox_value(field_id: "department")
hire_date: metabox_value(field_id: "start_date")
}
}
}
Navigation Menu Items:
{
nav_menu(identifier: "slug", value: "main-menu") {
items {
title
url
menu_icon: metabox_value(field_id: "menu_icon")
highlight_item: metabox_value(field_id: "featured_menu_item")
children @recursive {
title
url
menu_icon: metabox_value(field_id: "menu_icon")
}
}
}
}
Settings Pages (REQUIRES option_name):
{
# Meta Box settings pages - MUST include option_name parameter
site_logo: metabox_value(field_id: "company_logo", option_name: "site_settings")
contact_email: metabox_value(field_id: "main_contact_email", option_name: "site_settings")
# Group field on settings page
site_branding: metabox_group_value(field_id: "brand_settings", option_name: "site_settings") {
primary_color: metabox_value(field_id: "primary_color")
logo_dark: metabox_value(field_id: "logo_dark_mode")
}
# Multiple groups on settings page
social_links: metabox_groups_value(field_id: "social_media", option_name: "site_settings") {
platform: metabox_value(field_id: "platform_name")
url: metabox_value(field_id: "profile_url")
}
# Contact information group
contact_info: metabox_group_value(field_id: "contact_details", option_name: "site_settings") {
phone: metabox_value(field_id: "business_phone")
email: metabox_value(field_id: "business_email")
address: metabox_value(field_id: "business_address")
}
# Process global data
social_count: expression_result(
expression: "count(social_links)"
)
}
Key Context Rules
Posts: Meta Box fields accessed within post { ... } or posts { ... }
Terms: Meta Box fields accessed within term { ... } or terms { ... }
Users: Meta Box fields accessed within current_user { ... } or users { ... }
Navigation: Meta Box fields accessed within menu items { ... }
Settings: Meta Box fields require option_name parameter at root level
Settings pages always require option_name, object-level contexts never use option_name.
Advanced: Field Configuration Access
MetaBox Field Configuration
Like ACF's acf_field_object(), you might want to access MetaBox field configuration and metadata - information about how fields are set up rather than their data values.
Unlike for ACF, we don't provide a MetaBox dedicated metabox_field_object() method in Builderius. However, you can access field configuration using php_function_output Pro Feature to call MetaBox's native PHP functions directly.
Getting Field Configuration
Access field settings and metadata:
Get Field Configuration
Access individual field settings using MetaBox's API:
{
# Get field configuration
contact_field_config: php_function_output(
function: "rwmb_get_field_settings"
arguments: ["contact_email"]
)
# Get field configuration with context
image_field_config: php_function_output(
function: "rwmb_get_field_settings"
arguments: ["hero_image", {"object_type": "post"}, 123]
)
}
Use case: Access field type, validation rules, labels, and other configuration to build dynamic interfaces or validate input.
Get All Fields in a Meta Box
Retrieve all fields from a specific meta box:
{
# Get all fields for current post
post_metabox_fields: php_function_output(
function: "rwmb_get_object_fields"
arguments: ["my_metabox_id", "post"]
)
# Get all fields for specific object
contact_page_fields: php_function_output(
function: "rwmb_get_object_fields"
arguments: ["contact_metabox", "post", 456]
)
}
Use case: Build dynamic forms or iterate through all fields in a meta box programmatically.
Field Validation and Settings
Access field constraints and settings for validation:
{
# Get upload field constraints
image_settings: php_function_output(
function: "rwmb_get_field_settings"
arguments: ["profile_photo"]
)
# Get select field choices
dropdown_settings: php_function_output(
function: "rwmb_get_field_settings"
arguments: ["product_category"]
)
}
Use case: Display field requirements, show available options for select fields, or validate user input based on field configuration.
Common MetaBox API Functions
Available functions for php_function_output:
| Function | Purpose | Arguments |
|---|---|---|
rwmb_get_field_settings | Get field configuration | field_id, args, object_id |
rwmb_get_object_fields | Get all fields for object | object_type, object_id |
rwmb_get_registry | Access field registry | 'field' |
Field Value vs Field Configuration
metabox_value(field_id: "field_name")→ The actual data (what users entered)php_function_output(function: "rwmb_get_field_settings", arguments: ["field_name"])→ The configuration (how the field is set up)
Use MetaBox's PHP functions through php_function_output when you need field configuration data for building dynamic interfaces or validation logic.