How to add support for custom field with array of id's to other posts #2488
-
Based on this article: https://master--wpgraphql-docs.netlify.app/getting-started/custom-fields-and-meta/ I am able to set custom fields for a field that is a string and it works great. What I am trying to work out is the following Let's say I have 2 post types.
Articles has a custom field called reporters and the meta data of that field is stored as an array of reporter post id's "reporters": [
"10044679",
"10044673"
] This is the code I have so far
register_graphql_field( $post_type_object->graphql_single_name, 'reporters', [
'type' => 'String',
'description' => __( 'The color of the post', 'wp-graphql' ),
'resolve' => function( $post ) {
$reporters = get_post_meta( $post->ID, 'reporters' );
return ! empty( $reporters ) ? $reporters : [];
}
] ); Any help would be greatly appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
@andyjamesn you've registered your field as You can update this to return a list of strings by changing the You might also consider using a |
Beta Was this translation helpful? Give feedback.
@andyjamesn you've registered your field as
'type' => 'String'
which means you're promising to return a string, but the resolver is returning an array.You can update this to return a list of strings by changing the
type
to be'type' => [ 'list_of' => 'String' ]
You might also consider using a
connection
here and registering a connection between the Post Type and Reporters.see: register_graphql_connection