0

I am using wpgraphql to communicate with wordpress database. I have got one posts table in which I can see all the attributes of the post filled. There is a field for custom field too, I can see the value filled for custom field in the postMeta table ,but cannot get the meta field attribute in the subfield of the posts in the graphql query.

1 Answers1

0

Your meta still needs to be registered to the GraphQL schema before you can retrieve it from your request. Here's an example. Just replace PostTypeName, field_name, and FieldType with the what you need, put it in a theme's functions.php or a plugin to be run after plugins_loaded hook and before graphql_register_types hook and it should work.

add_action( 'graphql_register_types', function() {
    register_graphql_field( 'PostTypeName', 'field_name', [
        'type' => 'FieldType',
        'resolve' => function( $post ) {
            $meta = get_post_meta( $post->databaseId, 'meta_key'  );
            return ! empty( $meta ) ? $meta : null;
        }
    ]);
});

The WPGraphQL docs has more info on this here

Geoff Taylor
  • 496
  • 3
  • 17