My items from my array seem to not be passed into the my 'resolve' => function. Right now the function returns fail. If I replace $meta with an array item then it works. Im trying to fetch meta values for GraphQL and dont want to make a function per field.
add_action( 'graphql_register_types', function() {
$metas = array('phone', 'city', 'state', 'zip');
foreach($metas as $meta){
register_graphql_field( 'Location', $meta, [
'type' => 'String',
'description' => __( 'The post data', 'wp-graphql' ),
'resolve' => function($post, $meta) {
$GQL_data = get_post_meta( get_the_ID(), $meta, true);
return ! empty( $GQL_data ) ? $GQL_data : 'fail';
}
] );
}
});
Expected GraphQL output:
"phone": "123-123-1234",
"city": "Los Angeles",
"state": "CA",
"zip": "99922"
currently all items have a value of: "fail"