• So I have successfully created a custom post type like this

    add_action( 'init', 'create_post_type', 0 );

    function create_post_type() {

    register_post_type( 'codingblog',
    // CPT Options
    array(
    'labels' => array(
    'name' => __( 'CodingBlogs' ),
    'singular_name' => __( 'CodingBlog' ),
    'menu_name' => __( 'My Coding Blog', 'blogus'),
    'all_items' => __( 'All Coding Blogs', 'blogus'),
    'view_item' => __( 'View Coding Blog', 'blogus'),
    'add_new_item' => __( 'Add New Coding Blog', 'blogus'),
    'add_new' => __( 'Add New', 'blogus'),
    'edit_item' => __( 'Edit Coding Blog', 'blogus'),
    'update_item' => __( 'Update Coding Blog', 'blogus'),
    'search_items' => __( 'Search Coding Blog', 'blogus'),
    'not_found' => __( 'Not Found', 'blogus'),
    'not_found_in_trash' => __( 'Not found in Trash', 'blogus'),
    ),
    'label' => __( 'codingblogs', 'blogus' ),
    'description' => __( 'Coding Blog Posts', 'blogus'),
    'supports' => array( 'title', 'editor', 'excerpt',
    'author', 'thumbnail', 'comments',
    'revisions', 'custom-fields', ),
    'taxonomies' => array( 'post_tag', 'category', ),
    'hierarchical' => false,
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'show_in_nav_menus' => true,
    'show_in_admin_bar' => true,
    'menu_position' => 5,
    'can_export' => true,
    'has_archive' => true,
    'exclude_from_search' => false,
    'publicly_queryable' => true,
    'capability_type' => 'post',
    'rewrite' => array('slug' => 'codingblogs'),
    'show_in_rest' => true,
    )
    );
    }

    I was expecting (from what I read) that if I register a custom field with this custom type it will be visible in the editor when I edit an instance of this post type.

    function create_post_type_fields() {
    register_post_meta( 'codingblog', 'themeslug_github_repo', [
    // Add to the REST response
    'show_in_rest' => true,
    'single' => true,
    'type' => 'string',
    'sanitize_callback' => 'esc_url_raw',
    ] );
    }

    add_action( 'init', 'create_post_type_fields' );

    But this has not worked. As I went to measure of including the ‘custom-fields’ in the supports list for the custom post type I would have expected this to work.

    That said, when I have used a plugin for this purpose (CPTUI for example, with Advanced Custom Fields) I was forced to create a field group, and then add my custom field in the field group and then associate this with the custom post type. This is somehow divergent from the developer code examples I have found which don’t mention field groups at all.

    So my questions are, why is the above not working? As I quite like field groups for the editor display, how can I create a custom post type, with one field group and one custom post type like the one above? I genuinely cannot find a code example for this use case after much searching.

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    IIRC registering meta keys only serves to have the keys/values appear in the API. It does not impact the custom fields dialogs in the editor. You don’t need to register anything to use the editor dialogs, provided it’s part of what your post type supports. But unregistered entries will not appear in the API.

    If you want field group organization, you’d either need to develop your own editor meta box or use an appropriate custom fields plugin. The default custom fields dialogs do not support field groups.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.