Skip to content

Instantly share code, notes, and snippets.

@AliNaraghiA
Created July 11, 2023 13:13
Show Gist options
  • Save AliNaraghiA/b4497f274718f9c3c984798dfe8118a5 to your computer and use it in GitHub Desktop.
Save AliNaraghiA/b4497f274718f9c3c984798dfe8118a5 to your computer and use it in GitHub Desktop.
store post meta in WPGraphQL
add_action( 'graphql_register_types', function() {
register_graphql_field( 'Post', 'color', [
'type' => 'String',
'description' => __( 'The color of the post', 'wp-graphql' ),
'resolve' => function( $post ) {
$color = get_post_meta( $post->ID, 'color', true );
return ! empty( $color ) ? $color : 'blue';
}
] );
} );
export default {
async asyncData({ app }) {
const { data } = await app.apolloProvider.defaultClient.query({
query: gql`
{
posts {
nodes {
id
link
color
}
}
}
`,
});
const posts = data.posts.nodes;
console.log(posts[0].color); // Access the color field of the first post
return { posts };
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment