Skip to content

Instantly share code, notes, and snippets.

@oddevan

oddevan/graphql_custom_meta.php Secret

Last active Mar 15, 2021
Embed
What would you like to do?
<?php
add_action( 'graphql_register_types', function() {
register_graphql_field( 'Album', 'externalLinks', [
'type' => 'String',
'description' => __( 'Links to the album on external services', 'smolblog' ),
'resolve' => function( $post ) {
// Get the meta field
$links = get_post_meta( $post->ID, 'find_links', true );
// Return an empty JSON object if the meta field is empty
if (empty($links)) {
return '{}';
}
// If $links isn't an array (there's only one link), make it an array.
if ( ! is_array( $links ) ) {
$links = [ $links ];
}
// JSON encode the array and return it
return wp_json_encode( $links );
}
] );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment