Skip to main content

Post and Page Fields

How to get values from Post and Page Meta Box custom fields

info

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&param2=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.
info

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.

note

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{
queried_post{
custom_autocomplete: metabox_value(field_id: "custom_autocomplete")
}
}

Example for Background field:

query{
queried_post{
custom_background: metabox_value(field_id: "custom_background")
}
}

Example for Button Group field:

query{
queried_post {
custom_button_group: metabox_value(field_id: "custom_button_group")
}
}

Example for Checkbox List field:

query{
queried_post {
custom_checkbox_list: metabox_value(field_id: "custom_checkbox_list")
}
}

Example for Checkbox field:

query{
queried_post {
custom_checkbox: metabox_value(field_id: "custom_checkbox")
}
}

Example for Color Picker field:

query{
queried_post {
custom_color: metabox_value(field_id: "custom_color")
}
}

Example for Custom HTML field:

query{
queried_post {
custom_html: metabox_value(field_id: "custom_html")
}
}

Example for Date field:

query{
queried_post {
custom_date: metabox_value(field_id: "custom_date")
}
}

Example for Datetime field:

query{
queried_post {
custom_datetime: metabox_value(field_id: "custom_datetime")
}
}

Example for Fieldset Text field:

query{
queried_post {
custom_fieldset: metabox_value(field_id: "custom_fieldset")
}
}

Example for File Input field:

query{
queried_post {
custom_file_input: metabox_value(field_id: "custom_file_input")
}
}

Example for File field:

Same query and results valid for File Advanced, File Upload fields.

query{
queried_post {
custom_file: metabox_value(field_id: "custom_file")
}
}

Example for Image Select field:

query{
queried_post {
custom_image_select: metabox_value(field_id: "custom_image_select")
}
}

Example for Image field:

Same query and results valid for Image Advanced, Image Upload, Single Image fields.

query{
queried_post {
custom_image: metabox_value(field_id: "custom_image")
}
}

Example for Image field with specified size:

Same query and results valid for Image Advanced, Image Upload, Single Image fields.

query{
queried_post {
custom_image: metabox_value(field_id: "custom_image", args: "size=thumbnail")
}
}

Example for Post field (basic method):

query{
queried_post {
custom_post: metabox_value(field_id: "custom_post")
}
}

Example for Post field with single value (advanced method):

info

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{
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")
}
}
}

Example for Post field with multiple values (advanced method):

info

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{
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")
}
}
}

Example for Taxonomy field (basic method):

query{
queried_post {
custom_taxonomy: metabox_value(field_id: "custom_taxonomy")
}
}

Example for Taxonomy field with single value (advanced method):

Same query and results valid for Taxonomy Advanced field.

info

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{
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")
}
}
}

Example for Taxonomy field with multiple values (advanced method):

info

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{
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")
}
}
}

Example for User field (basic method):

query{
queried_post {
custom_user: metabox_value(field_id: "custom_user")
}
}

Example for User field with single value (advanced method):

info

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{
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")
}
}
}

Example for User field with multiple values (advanced method):

info

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{
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")
}
}
}

Example for Group field (basic method):

query{
queried_post {
custom_group: metabox_value(field_id: "custom_group")
}
}

Example for Group field with single value (advanced method):

info

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{
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
}
}
}
}

Example for Group field with multiple values (advanced method):

info

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{
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
}
}
}
}