Custom fields allow you to add metadata to your posts. The CustomField class in the theme makes it simple to create and manage custom fields for any post type.
This feature is disabled by default. To enable it, set the GRAPHQL_STARTER_ENABLE_CUSTOM_FIELDS constant to true in the config.php file.
Note: Please enable this feature by setting the
GRAPHQL_STARTER_ENABLE_CUSTOM_FIELDSconstant to true in theconfig.phpfile.
When registering a custom field, the following parameters are required:
CustomField::register([
'id' => 'field_id', // Unique identifier for the field
'title' => 'Field Title', // Display title in admin
'post_types' => ['post'], // Array of post types to add this field to
]);
CustomField::register([
// Optional parameters with their defaults
'context' => 'normal', // Where the field appears: 'normal', 'side', or 'advanced'
'priority' => 'default', // Display priority: 'high', 'core', 'default', or 'low'
'show_in_graphql' => true, // Whether to expose in GraphQL
'description' => '', // Field description (used in GraphQL docs)
]);
CustomField::register([
'id' => 'text_field',
'title' => 'Text Field',
'post_types' => ['post'],
'input_type' => [
'type' => 'text',
'attributes' => [
'placeholder' => 'Enter text...',
'maxlength' => '100',
// Any valid HTML input attributes
],
],
]);
CustomField::register([
'id' => 'email_field',
'title' => 'Email Address',
'post_types' => ['contact'],
'input_type' => [
'type' => 'email',
'attributes' => [
'placeholder' => 'email@example.com',
],
],
]);
CustomField::register([
'id' => 'website',
'title' => 'Website URL',
'post_types' => ['profile'],
'input_type' => [
'type' => 'url',
'attributes' => [
'placeholder' => 'https://',
],
],
]);
CustomField::register([
'id' => 'quantity',
'title' => 'Quantity',
'post_types' => ['product'],
'input_type' => [
'type' => 'number',
'attributes' => [
'min' => '0',
'max' => '100',
'step' => '1',
],
],
]);
CustomField::register([
'id' => 'phone',
'title' => 'Phone Number',
'post_types' => ['contact'],
'input_type' => [
'type' => 'tel',
'attributes' => [
'pattern' => '[0-9]{3}-[0-9]{3}-[0-9]{4}',
'placeholder' => '123-456-7890',
],
],
]);
CustomField::register([
'id' => 'event_date',
'title' => 'Event Date',
'post_types' => ['event'],
'input_type' => [
'type' => 'date',
'attributes' => [
'min' => '2024-01-01',
'max' => '2024-12-31',
],
],
]);
CustomField::register([
'id' => 'description',
'title' => 'Description',
'post_types' => ['product'],
'input_type' => [
'type' => 'textarea',
'attributes' => [
'rows' => '5',
'placeholder' => 'Enter detailed description...',
],
],
]);
CustomField::register([
'id' => 'size',
'title' => 'Size',
'post_types' => ['product'],
'input_type' => [
'type' => 'radio',
'requires_options' => true,
],
'options' => [
'small' => 'Small',
'medium' => 'Medium',
'large' => 'Large',
],
]);
CustomField::register([
'id' => 'features',
'title' => 'Features',
'post_types' => ['product'],
'input_type' => [
'type' => 'checkbox',
'requires_options' => true,
'is_multiple' => true,
],
'options' => [
'wifi' => 'WiFi',
'bluetooth' => 'Bluetooth',
'gps' => 'GPS',
],
]);
All custom post types and fields registered using the CustomPostType and CustomField classes are automatically exposed in the WPGraphQL schema. This allows you to query these custom post types and their associated metadata using GraphQL.
Example Query:
{
products {
nodes {
title
customFieldSize
customFieldFeatures
}
}
}
custom-post-types.php and custom-fields.php files, respectively.The CustomField class will throw an InvalidArgumentException if:
For further details, refer to the main GraphQL Starter Documentation.