johnbillion / extended-cpts

A library which provides extended functionality to WordPress custom post types and taxonomies.
GNU General Public License v2.0
973 stars 97 forks source link

Register fields with WP GraphQL #210

Open troyblakelydc opened 1 year ago

troyblakelydc commented 1 year ago

I am trying to register custom fields to the wp-graphql plugin with ACF. Per the documentation, fields can be registered in PHP by adding: 'show_in_graphql' => true, 'graphql_field_name' => 'myGroup',

I added these attributes to the admin_cols collection of the custom post type using poet, and it doesn't seem to be recognized by wp-graphql. I'm not sure if this is an issue with poet, extended-cpts or wp-graphql, but any suggestions would be appreciated.

johnbillion commented 1 year ago

Did you put these fields at the top level of the arguments array used for the post type generation? They don't go into the admin_cols field.

troyblakelydc commented 1 year ago

We have a custom post type called "release" that we have flagged to 'show_in_graphql' as shown below. This successfully adds the custom post type to the GQL schema. Everything works there.

This is through poet, so I know it works a little differently, just hoping you might have an idea.

'post' => [
        'release' => [
            'supports' => ['title', 'thumbnail'],
            'show_in_rest' => true,
            'labels' => [
                'singular' => 'Release',
                'plural' => 'Releases',
            ],
            'admin_cols' => [...]
            ],
            'show_in_graphql' => true,
            'graphql_single_name' => 'release',
            'graphql_plural_name' => 'releases',
        ],

However, each custom field that you want exposed to the GQL schema also needs to be explicitly added. The documentation shows this being done as the field group is added to ACF. I'm not sure if/how this might map into poet. I tried adding it into the admin_cols section both as an attribute of admin_cols, and as attributes of a give field and neither one worked.

'admin_cols' => [
                'artist' => ['post_field' => 'artist', 
                'meta_key' => 'artist',
                'show_in_graphql' => true,
                'graphql_field_name' => 'artist_group',],
                ...
                ],
                'show_in_graphql' => true,
                'graphql_field_name' => 'admin_cols',
            ],