extended-cpts icon indicating copy to clipboard operation
extended-cpts copied to clipboard

Register fields with WP GraphQL

Open troyblakelydc opened this issue 2 years ago • 2 comments

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.

troyblakelydc avatar Apr 28 '23 21:04 troyblakelydc

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.

johnbillion avatar May 01 '23 11:05 johnbillion

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',
            ],

troyblakelydc avatar May 04 '23 18:05 troyblakelydc