graphql-starter

Custom Post Types

Custom Post Types (CPTs) allow you to extend WordPress by adding new content types beyond the default ones (Posts, Pages, etc.). With the GraphQL Starter theme, you can register CPTs easily using the built-in CustomPostType class.

This feature is disabled by default. To enable it, set the GRAPHQL_STARTER_ENABLE_CUSTOM_POST_TYPES constant to true in the config.php file.

Note: Please enable this feature by setting the GRAPHQL_STARTER_ENABLE_CUSTOM_POST_TYPES constant to true in the config.php file.

Registering Custom Post Types

Example 1: Basic Post Type

CustomPostType::register(['slug' => 'project']);

This creates a custom post type with the slug project. When only providing the slug:

Example 2: Post Type with Custom Settings

CustomPostType::register([
    'slug' => 'event',
    'singular_name' => 'Event',    // Override the auto-generated singular name
    'plural_name' => 'Events',     // Override the auto-generated plural name
    'supports' => ['title', 'editor', 'thumbnail', 'excerpt'],
    'menu_icon' => 'dashicons-calendar',
    'menu_position' => 7,
    'has_archive' => true,
]);

This registers a post type with custom settings that override the automatic defaults.

Default Behavior

The CustomPostType class will automatically:

You only need to specify custom settings when you want to override these defaults.

Available Configuration Options

When registering a custom post type, you can use the following options:

CustomPostType::register([
    // Required
    'slug' => 'book',              // String: Unique identifier for the post type
    
    // Basic Settings
    'singular_name' => 'Book',     // String: Singular display name
    'plural_name' => 'Books',      // String: Plural display name
    'public' => true,              // Boolean: Whether the post type is publicly accessible
    'has_archive' => false,        // Boolean: Whether the post type has archive pages
    'hierarchical' => false,       // Boolean: Whether posts can have parent/child relationships
    
    // Features Support
    'supports' => [                // Array: Post type features to enable
        'title',                   // Post title
        'editor',                  // Content editor
        'thumbnail',               // Featured image
        'excerpt',                 // Post excerpt
        'author',                  // Author selection
        'custom-fields',          // Custom fields
        'page-attributes',        // Page attributes (menu order, etc.)
    ],
    
    // Admin UI
    'menu_icon' => 'dashicons-book-alt',  // String: Dashicon or URL for admin menu icon
    'menu_position' => 5,         // Integer: Position in admin menu (5-100)
    
    // Visibility & Access
    'publicly_queryable' => true,  // Boolean: Whether queries can be performed on the front end
    'exclude_from_search' => false, // Boolean: Whether to exclude from search results
    
    // REST API & GraphQL
    'show_in_rest' => true,        // Boolean: Whether to show in REST API. Recommended to be true when editor is enabled.
    'show_in_graphql' => true,     // Boolean: Whether to show in GraphQL API
    'graphql_single_name' => 'book',  // String: GraphQL singular name
    'graphql_plural_name' => 'books',  // String: GraphQL plural name
    
    // Custom Labels
    'labels' => [                  // Array: Custom labels for various UI text
        'menu_name' => 'Books',
        'add_new' => 'Add New',
        'add_new_item' => 'Add New Book',
        'edit_item' => 'Edit Book',
        'new_item' => 'New Book',
        'view_item' => 'View Book',
        'view_items' => 'View Books',
        'search_items' => 'Search Books',
        'not_found' => 'No books found',
        'not_found_in_trash' => 'No books found in trash',
        'all_items' => 'All Books',
        'archives' => 'Book Archives',
        'attributes' => 'Book Attributes',
        // ... see WordPress documentation for all available labels
    ],
]);

Option Details

Basic Settings

Features Support

The supports array can include any of these values:

Admin UI Options

Visibility Settings

API Integration