Post and Page Fields
How to get values from Post and Page Meta Box custom fields
Meta Box fields available in PRO
version only.
On this page will be shown examples how to get values of Meta Box custom fields related to Posts and Pages.
We have to use metabox_value
field of GraphQL Post type to get value of any Meta Box field type.
This field is created based on [rwmb_get_field](https://docs.Meta Box.io/functions/rwmb-get-value/) Meta Box function and takes two arguments:
- field_id - here should be used
ID
setting value of the Meta Box custom field - args - extra arguments for some field types (like size for image_advanced) or for another object type (like getting values for terms or users). Format
param1=value1¶m2=value2
.
Returned value:
- If the field has a single value (not multiple nor clone), then the function returns that value.
- If the field has multiple values (multiple, clone, or the field type is group), then the function returns an array of values.
Depending on the field types, the returned value can be different. Please refer to each field type in the [Fields](https://docs.Meta Box.io/fields/) section in Meta Box documentation for more details.
Some Meta Box field types(Post, Taxonomy, User, Group) have their own specific GraphQL fields to get values in advanced way.
For all examples will be used queried_post
GraphQL field, but same approach valid for any GraphQL field of Post type.
Example for Autocomplete field:
- Query
- Result
query{
queried_post{
custom_autocomplete: metabox_value(field_id: "custom_autocomplete")
}
}
- Non-Cloneable
- Cloneable
{
"queried_post": {
"custom_autocomplete": [
0: "css",
1: "javascript"
]
}
}
{
"queried_post": {
"custom_autocomplete": [
0: [
0: "css",
1: "javascript"
],
1: [
0: "javascript"
]
]
}
}
Example for Background field:
- Query
- Result
query{
queried_post{
custom_background: metabox_value(field_id: "custom_background")
}
}
- Non-Cloneable
- Cloneable
{
"queried_post": {
"custom_background": {
"color": "#111222",
"image": "https://domain.com/wp-uploads/2017/12/bg.png",
"position": "top left",
"attachment": "fixed",
"size": "cover",
"repeat": "no-repeat"
}
}
}
{
"queried_post": {
"custom_background": [
0: {
"color": "#111222",
"image": "https://domain.com/wp-uploads/2017/12/bg1.png",
"position": "top left",
"attachment": "fixed",
"size": "cover",
"repeat": "no-repeat"
},
1: {
"color": "#111333",
"image": "https://domain.com/wp-uploads/2017/12/bg2.png",
"position": "top left",
"attachment": "fixed",
"size": "cover",
"repeat": "no-repeat"
}
]
}
}
Example for Button Group field:
- Query
- Result
query{
queried_post {
custom_button_group: metabox_value(field_id: "custom_button_group")
}
}
- Non-Cloneable(single)
- Non-Cloneable(multiple)
- Cloneable(single)
- Cloneable(multiple)
{
"queried_post": {
"custom_button_group": "html"
}
}
{
"queried_post": {
"custom_button_group": [
0: "css",
1: "html"
]
}
}
{
"queried_post": {
"custom_button_group": [
0: "css",
1: "html"
]
}
}
{
"queried_post": {
"custom_button_group": [
0: [
0: "css",
1: "javascript"
],
1: [
0: "javascript"
]
]
}
}
Example for Checkbox List field:
- Query
- Result
query{
queried_post {
custom_checkbox_list: metabox_value(field_id: "custom_checkbox_list")
}
}
- Non-Cloneable
- Cloneable
{
"queried_post": {
"custom_checkbox_list": [
0: "css",
1: "javascript"
]
}
}
{
"queried_post": {
"custom_checkbox_list": [
0: [
0: "css",
1: "javascript"
],
1: [
0: "javascript"
]
]
}
}
Example for Checkbox field:
- Query
- Result
query{
queried_post {
custom_checkbox: metabox_value(field_id: "custom_checkbox")
}
}
- Non-Cloneable
- Cloneable
{
"queried_post": {
"custom_checkbox": 0
}
}
{
"queried_post": {
"custom_checkbox": [
0: 1
]
}
}
Example for Color Picker field:
- Query
- Result
query{
queried_post {
custom_color: metabox_value(field_id: "custom_color")
}
}
- Non-Cloneable
- Cloneable
{
"queried_post": {
"custom_color": "#c9882e"
}
}
{
"queried_post": {
"custom_color": [
0: "#c9882e",
1: "#219128"
]
}
}
Example for Custom HTML field:
- Query
- Result
query{
queried_post {
custom_html: metabox_value(field_id: "custom_html")
}
}
{
"queried_post": {
"custom_html": "<div class=\"alert alert-warning\">This is a custom HTML content</div>"
}
}
Example for Date field:
- Query
- Result
query{
queried_post {
custom_date: metabox_value(field_id: "custom_date")
}
}
- Non-Cloneable
- Cloneable
{
"queried_post": {
"custom_date": "30-01-2019"
}
}
{
"queried_post": {
"custom_date": [
0: "25-01-2019",
1: "30-01-2019"
]
}
}
Example for Datetime field:
- Query
- Result
query{
queried_post {
custom_datetime: metabox_value(field_id: "custom_datetime")
}
}
- Non-Cloneable
- Cloneable
{
"queried_post": {
"custom_datetime": "30-01-2019 09:30:00"
}
}
{
"queried_post": {
"custom_datetime": [
0: "25-01-2019 09:30:00",
1: "30-01-2019 09:30:00"
]
}
}
Example for Fieldset Text field:
- Query
- Result
query{
queried_post {
custom_fieldset: metabox_value(field_id: "custom_fieldset")
}
}
- Non-Cloneable
- Cloneable
{
"queried_post": {
"custom_fieldset": {
"color": "#111222",
"position": "top left",
"attachment": "fixed",
"size": "cover"
}
}
}
{
"queried_post": {
"custom_fieldset": [
0: {
"color": "#111222",
"position": "top left",
"attachment": "fixed",
"size": "cover"
},
1: {
"color": "#111333",
"position": "top left",
"attachment": "fixed",
"size": "cover"
}
]
}
}
Example for File Input field:
- Query
- Result
query{
queried_post {
custom_file_input: metabox_value(field_id: "custom_file_input")
}
}
- Non-Cloneable
- Cloneable
{
"queried_post": {
"custom_file_input": "url": "https://example.com/wp-content/uploads/intro.txt"
}
}
{
"queried_post": {
"custom_file_input": [
0: "https://example.com/wp-content/uploads/intro.txt",
1: "https://example.com/wp-content/uploads/intro2.txt"
]
}
}
Example for File field:
Same query and results valid for File Advanced
, File Upload
fields.
- Query
- Result
query{
queried_post {
custom_file: metabox_value(field_id: "custom_file")
}
}
- Non-Cloneable
- Cloneable
{
"queried_post": {
"custom_file": {
"ID": 123,
"name": "intro.txt",
"path": "/var/www/wp-content/uploads/intro.txt",
"url": "https://example.com/wp-content/uploads/intro.txt",
"title": "Introduction"
}
}
}
{
"queried_post": {
"custom_file": [
0: {
"ID": 123,
"name": "intro.txt",
"path": "/var/www/wp-content/uploads/intro.txt",
"url": "https://example.com/wp-content/uploads/intro.txt",
"title": "Introduction"
},
1: {
"ID": 124,
"name": "intro2.txt",
"path": "/var/www/wp-content/uploads/intro2.txt",
"url": "https://example.com/wp-content/uploads/intro2.txt",
"title": "Introduction2"
}
]
}
}
Example for Image Select field:
- Query
- Result
query{
queried_post {
custom_image_select: metabox_value(field_id: "custom_image_select")
}
}
- Non-Cloneable(single)
- Non-Cloneable(multiple)
- Cloneable(single)
- Cloneable(multiple)
{
"queried_post": {
"custom_image_select": "https://example.com/wp-content/uploads/logo-150x80.png"
}
}
{
"queried_post": {
"custom_image_select": [
0: "https://example.com/wp-content/uploads/logo-150x80.png",
1: "https://example.com/wp-content/uploads/logo-150x150.png"
]
}
}
{
"queried_post": {
"custom_image_select": [
0: "https://example.com/wp-content/uploads/logo-150x80.png",
1: "https://example.com/wp-content/uploads/logo-150x150.png"
]
}
}
{
"queried_post": {
"custom_image_select": [
0: [
0: "https://example.com/wp-content/uploads/logo-150x80.png",
1: "https://example.com/wp-content/uploads/logo-150x150.png"
],
1: [
0: "https://example.com/wp-content/uploads/logo-150x150.png"
]
]
}
}
Example for Image field:
Same query and results valid for Image Advanced
, Image Upload
, Single Image
fields.
- Query
- Result
query{
queried_post {
custom_image: metabox_value(field_id: "custom_image")
}
}
- Non-Cloneable
- Cloneable
{
"queried_post": {
"custom_image": {
"ID" : 123,
"name": "logo-150x80.png",
"path": "/var/www/wp-content/uploads/logo-150x80.png",
"url": "https://example.com/wp-content/uploads/logo-150x80.png",
"width": 150,
"height": 80,
"full_url": "https://example.com/wp-content/uploads/logo.png",
"title": "Logo",
"caption": "Logo caption",
"description": "Used in the header",
"alt": "Logo ALT text",
"srcset": "large.jpg 1920w, medium.jpg 960w, small.jpg 480w",
"sizes": [],
"image_meta": []
}
}
}
{
"queried_post": {
"custom_image": [
0: {
"ID" : 123,
"name": "logo-150x80.png",
"path": "/var/www/wp-content/uploads/logo-150x80.png",
"url": "https://example.com/wp-content/uploads/logo-150x80.png",
"width": 150,
"height": 80,
"full_url": "https://example.com/wp-content/uploads/logo.png",
"title": "Logo",
"caption": "Logo caption",
"description": "Used in the header",
"alt": "Logo ALT text",
"srcset": "large.jpg 1920w, medium.jpg 960w, small.jpg 480w",
"sizes": [],
"image_meta": []
},
1: {
"ID" : 124,
"name": "logo2-150x80.png",
"path": "/var/www/wp-content/uploads/logo2-150x80.png",
"url": "https://example.com/wp-content/uploads/logo2-150x80.png",
"width": 150,
"height": 80,
"full_url": "https://example.com/wp-content/uploads/logo2.png",
"title": "Logo",
"caption": "Logo caption",
"description": "Used in the header",
"alt": "Logo ALT text",
"srcset": "large.jpg 1920w, medium.jpg 960w, small.jpg 480w",
"sizes": [],
"image_meta": []
}
]
}
}
Example for Image field with specified size:
Same query and results valid for Image Advanced
, Image Upload
, Single Image
fields.
- Query
- Result
query{
queried_post {
custom_image: metabox_value(field_id: "custom_image", args: "size=thumbnail")
}
}
- Non-Cloneable
- Cloneable
{
"queried_post": {
"custom_image": {
"ID" : 123,
"name": "logo-150x80.png",
"path": "/var/www/wp-content/uploads/logo-150x80.png",
"url": "https://example.com/wp-content/uploads/logo-150x80.png",
"width": 150,
"height": 80,
"full_url": "https://example.com/wp-content/uploads/logo.png",
"title": "Logo",
"caption": "Logo caption",
"description": "Used in the header",
"alt": "Logo ALT text",
"srcset": "large.jpg 1920w, medium.jpg 960w, small.jpg 480w",
"sizes": [],
"image_meta": []
}
}
}
{
"queried_post": {
"custom_image": [
0: {
"ID" : 123,
"name": "logo-150x80.png",
"path": "/var/www/wp-content/uploads/logo-150x80.png",
"url": "https://example.com/wp-content/uploads/logo-150x80.png",
"width": 150,
"height": 80,
"full_url": "https://example.com/wp-content/uploads/logo.png",
"title": "Logo",
"caption": "Logo caption",
"description": "Used in the header",
"alt": "Logo ALT text",
"srcset": "large.jpg 1920w, medium.jpg 960w, small.jpg 480w",
"sizes": [],
"image_meta": []
},
1: {
"ID" : 124,
"name": "logo2-150x80.png",
"path": "/var/www/wp-content/uploads/logo2-150x80.png",
"url": "https://example.com/wp-content/uploads/logo2-150x80.png",
"width": 150,
"height": 80,
"full_url": "https://example.com/wp-content/uploads/logo2.png",
"title": "Logo",
"caption": "Logo caption",
"description": "Used in the header",
"alt": "Logo ALT text",
"srcset": "large.jpg 1920w, medium.jpg 960w, small.jpg 480w",
"sizes": [],
"image_meta": []
}
]
}
}
Example for Post field (basic method):
- Query
- Result
query{
queried_post {
custom_post: metabox_value(field_id: "custom_post")
}
}
- Non-Cloneable(single)
- Non-Cloneable(multiple)
- Cloneable(single)
- Cloneable(multiple)
{
"queried_post": {
"custom_post": 123
}
}
{
"queried_post": {
"custom_post": [
0: 123,
1: 124
]
}
}
{
"queried_post": {
"custom_post": [
0: 123,
1: 124
]
}
}
{
"queried_post": {
"custom_post": [
0: [
0: 123,
1: 124
],
1: [
0: 130
]
]
}
}
Example for Post field with single value (advanced method):
GraphQL Post type has field metabox_post_value
which purpose is to get value of non-cloneable and non-multiple Meta Box Post field.
But comparing to metabox_value
(which returns just Post ID) - metabox_post_value
returns Post GraphQL type.
This method gives us more power and flexibility:
- we can get just necessary fields of Post
- we can get meta values of this Post
- we can get FeaturedImage of this Post
- we can get own Meta Box fields of this Post
- Query
- Result
query{
queried_post {
custom_post_object: metabox_post_value(field_id: "custom_post") {
ID
post_title
post_content
some_meta_key: meta_value(key: "some_meta_key")
custom_metabox_value: metabox_value(field_id: "custom_metabox_field_name")
}
}
}
{
"queried_post": {
"custom_post": {
"ID": 53,
"post_title": "Sequi qui blanditiis provident",
"post_content": " <here will be rendered post content, Gutenberg blocks will be rendered>",
"some_meta_key": "<here will be meta value>",
"custom_metabox_value": "<here will be value of Meta Box field>"
}
}
}
Example for Post field with multiple values (advanced method):
GraphQL Post type has field metabox_multiple_post_value
which purpose is to get value of cloneable or multiple Meta Box Post field.
But comparing to metabox_value
(which returns array of Post IDs if Meta Box Post field is cloneable or multiple) - metabox_multiple_post_value
returns array of Post GraphQL types.
- Query
- Result
query{
queried_post {
custom_multiple_post: metabox_multiple_post_value(field_id: "custom_multiple_post") {
ID
post_title
post_content
some_meta_key: meta_value(key: "some_meta_key")
custom_metabox_value: metabox_value(field_id: "custom_metabox_field_name")
}
}
}
{
"queried_post": {
"custom_multiple_post": [
0: {
"ID": 53,
"post_title": "Sequi qui blanditiis provident",
"post_content": " <here will be rendered post content, Gutenberg blocks will be rendered>",
"some_meta_key": "<here will be meta value>",
"custom_metabox_value": "<here will be value of Meta Box field>"
},
1: {
"ID": 54,
"post_title": "Sequi qui blanditiis",
"post_content": " <here will be rendered post content, Gutenberg blocks will be rendered>",
"some_meta_key": "<here will be meta value>",
"custom_metabox_value": "<here will be value of Meta Box field>"
}
]
}
}
Example for Taxonomy field (basic method):
- Query
- Result
query{
queried_post {
custom_taxonomy: metabox_value(field_id: "custom_taxonomy")
}
}
- Non-Multiple
- Multiple
{
"queried_post": {
"custom_taxonomy": {
"term_id":1
"name":"Uncategorized"
"slug":"uncategorized"
"term_group":0
"term_taxonomy_id":1
"taxonomy":"category"
"description":""
"parent":0
"count":21
"filter":"raw"
}
}
}
{
"queried_post": {
"custom_taxonomy": [
0:{
"term_id":1
"name":"Uncategorized"
"slug":"uncategorized"
"term_group":0
"term_taxonomy_id":1
"taxonomy":"category"
"description":""
"parent":0
"count":21
"filter":"raw"
}
]
}
}
Example for Taxonomy field with single value (advanced method):
Same query and results valid for Taxonomy Advanced
field.
GraphQL Post type has field "metabox_taxonomy_value" which purpose is to get value of non-multiple Meta Box Taxonomy field.
But comparing to "metabox_value" - "metabox_taxonomy_value" returns Term GraphQL type.
This method gives us more power and flexibility:
- we can get just necessary fields of Term
- we can get meta values of this Term
- we can get own Meta Box fields of this Term
- Query
- Result
query{
queried_post {
custom_taxonomy: metabox_taxonomy_value(field_id: "custom_taxonomy") {
term_id
slug
description
some_meta_key: meta_value(key: "some_meta_key")
custom_metabox_value: metabox_value(field_id: "custom_metabox_field_name")
}
}
}
{
"queried_post": {
"custom_term": {
"term_id": 1,
"slug": "uncategorized",
"description": "Uncategorized",
"some_meta_key": "<here will be meta value>",
"custom_metabox_value": "<here will be value of Meta Box field>"
}
}
}
Example for Taxonomy field with multiple values (advanced method):
GraphQL RootQuery type has field "metabox_multiple_taxonomy_value" which purpose is to get value of multiple Meta Box Taxonomy field.
"metabox_multiple_taxonomy_value" always returns array of GraphQL Term types(of course if field is multiple).
- Query
- Result
query{
queried_post {
custom_taxonomy: metabox_multiple_taxonomy_value(field_id: "custom_taxonomy") {
term_id
slug
description
some_meta_key: meta_value(key: "some_meta_key")
custom_metabox_value: metabox_value(field_id: "custom_metabox_field_name")
}
}
}
{
"queried_post": {
"custom_term":[
0:{
"term_id":1,
"slug":"uncategorized",
"description":"Uncategorized",
"some_meta_key":"<here will be meta value>",
"custom_metabox_value":"<here will be value of Meta Box field>"
},
1:{
"term_id":2,
"slug":"categorized",
"description":"Categorized",
"some_meta_key":"<here will be meta value>",
"custom_metabox_value":"<here will be value of Meta Box field>"
}
]
}
}
Example for User field (basic method):
- Query
- Result
query{
queried_post {
custom_user: metabox_value(field_id: "custom_user")
}
}
- Non-Cloneable(single)
- Non-Cloneable(multiple)
- Cloneable(single)
- Cloneable(multiple)
{
"queried_post": {
"custom_user": 123
}
}
{
"queried_post": {
"custom_user": [
0: 123,
1: 124
]
}
}
{
"queried_post": {
"custom_user": [
0: 123,
1: 124
]
}
}
{
"queried_post": {
"custom_user": [
0: [
0: 123,
1: 124
],
1: [
0: 130
]
]
}
}
Example for User field with single value (advanced method):
GraphQL Post type has field "metabox_user_value" which purpose is to get value of non-cloneable and non-multiple Meta Box User field.
But comparing to metabox_value
(which returns just User ID) - metabox_post_value
returns User GraphQL type.
This method gives us more power and flexibility:
- we can get just necessary fields of User
- we can get meta values of this User
- we can get own Meta Box fields of this User
- Query
- Result
query{
queried_post {
custom_user: metabox_user_value(field_id: "custom_user") {
ID
display_name
user_email
some_meta_key: meta_value(key: "some_meta_key")
custom_metabox_value: metabox_value(field_id: "custom_metabox_field_name")
}
}
}
{
"queried_post": {
"custom_user": {
"ID": 1,
"display_name": "admin",
"user_email": "[email protected]",
"some_meta_key": "<here will be meta value>",
"custom_metabox_value": "<here will be value of Meta Box field>"
}
}
}
Example for User field with multiple values (advanced method):
GraphQL RootQuery type has field "metabox_multiple_user_value" which purpose is to get value of cloneable or multiple Meta Box User field.
But comparing to metabox_value
(which returns array of User IDs if Meta Box Post field is cloneable or multiple) - metabox_multiple_user_value
returns array of User GraphQL types.
- Query
- Result
query{
queried_post {
custom_multiple_users: metabox_multiple_user_value(field_id: "custom_user") {
ID
display_name
user_email
some_meta_key: meta_value(key: "some_meta_key")
custom_metabox_value: metabox_value(field_id: "custom_metabox_field_name")
}
}
}
{
"queried_post": {
"custom_multiple_users":[
{
"ID":1,
"display_name":"admin",
"user_email":"[email protected]",
"some_meta_key":"<here will be meta value>",
"custom_metabox_value":"<here will be value of Meta Box field>"
},
{
"ID":2,
"display_name":"admin2",
"user_email":"[email protected]",
"some_meta_key":"<here will be meta value>",
"custom_metabox_value":"<here will be value of Meta Box field>"
}
]
}
}
Example for Group field (basic method):
- Query
- Result
query{
queried_post {
custom_group: metabox_value(field_id: "custom_group")
}
}
- Non-Cloneable
- Cloneable
{
"queried_post": {
"custom_group": {
"text": "My custom text",
"post": 123,
"user": 1
}
}
}
{
"queried_post": {
"custom_group": [
0:{
"text": "My custom text1",
"post": 123,
"user": 1
},
1:{
"text": "My custom text2",
"post": 124,
"user": 2
}
]
}
}
Example for Group field with single value (advanced method):
GraphQL Post type has field "metabox_group_value" which purpose is to get value of non-cloneable Meta Box Group field.
But comparing to "metabox_value" - "metabox_group_value" returns GraphQL MetaBoxGroupField type(MetaBoxGroupField has all Meta Box related GraphQL fields).
This method gives us more power and flexibility:
- we can get just necessary sub fields of MetaBoxGroupField
- if one of sub fields has Meta Box Post type, we can use
metabox_post_value
to get value of this sub field - if one of sub fields has Meta Box Taxonomy type, we can use
metabox_taxonomy_value
to get value of this sub field - if one of sub fields has Meta Box User type, we can use
metabox_user_value
to get value of this sub field - if one of sub fields has Meta Box Group type, we can use
metabox_group_value
to get value of this sub field
- Query
- Result
query{
queried_post {
custom_group: metabox_group_value(field_id: "custom_group") {
text: metabox_value(field_id: "text")
post: metabox_post_value(field_id: "post_object") {
ID
post_title
post_content
some_meta_key: meta_value(key: "some_meta_key")
custom_metabox_value: metabox_value(field_id: "custom_metabox_field_name")
}
user: metabox_user_value(field_id: "user") {
ID
display_name
user_email
}
}
}
}
{
"queried_post": {
"custom_group": {
"text": "custom text",
"post": {
"ID": 47,
"post_title": "Omnis nesciunt quasi amet officiis",
"post_content": "<here will be rendered post content, Gutenberg blocks will be rendered>",
"some_meta_key": "<here will be meta value>",
"custom_metabox_value": "<here will be value of Meta Box field>"
},
"user": {
"ID": 1,
"display_name": "admin",
"user_email": "[email protected]"
}
}
}
}
Example for Group field with multiple values (advanced method):
GraphQL Post type has field "metabox_multiple_group_value" which purpose is to get value of cloneable Meta Box Group field.
But comparing to "metabox_value" - "metabox_multiple_group_value" returns array of GraphQL MetaBoxGroupField types(MetaBoxGroupField has all Meta Box related GraphQL fields).
This method gives us more power and flexibility:
- we can get just necessary sub fields of MetaBoxGroupField
- if one of sub fields has Meta Box Post type, we can use
metabox_post_value
to get value of this sub field - if one of sub fields has Meta Box Taxonomy type, we can use
metabox_taxonomy_value
to get value of this sub field - if one of sub fields has Meta Box User type, we can use
metabox_user_value
to get value of this sub field - if one of sub fields has Meta Box Group type, we can use
metabox_group_value
to get value of this sub field
- Query
- Result
query{
queried_post {
custom_group: metabox_multiple_group_value(field_id: "custom_group") {
text: metabox_value(field_id: "text")
post: metabox_post_value(field_id: "post_object") {
ID
post_title
post_content
some_meta_key: meta_value(key: "some_meta_key")
custom_metabox_value: metabox_value(field_id: "custom_metabox_field_name")
}
user: metabox_user_value(field_id: "user") {
ID
display_name
user_email
}
}
}
}
{
"queried_post": {
"custom_group": [
0:{
"text": "custom text",
"post": {
"ID": 47,
"post_title": "Omnis nesciunt quasi amet officiis",
"post_content": "<here will be rendered post content, Gutenberg blocks will be rendered>",
"some_meta_key": "<here will be meta value>",
"custom_metabox_value": "<here will be value of Meta Box field>"
},
"user": {
"ID": 1,
"display_name": "admin",
"user_email": "[email protected]"
}
}
}
]
}