Skip to content

Instantly share code, notes, and snippets.

@jasonbahl
Created August 3, 2020 16:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonbahl/95f63db2edd7c0ef11c1afc44afc61e8 to your computer and use it in GitHub Desktop.
Save jasonbahl/95f63db2edd7c0ef11c1afc44afc61e8 to your computer and use it in GitHub Desktop.
add_action( 'graphql_register_types', function() {
register_graphql_connection([
'fromType' => 'Tag',
'toType' => 'ContentNode',
'fromFieldName' => 'contentNodes',
'resolve' => function( \WPGraphQL\Model\Term $source, $args, $context, $info ) {
// Get all post types allowed in GraphQL
$post_types = WPGraphQL::get_allowed_post_types();
// Instantiate a new PostObjectConnectionResolver class
$resolver = new \WPGraphQL\Data\Connection\PostObjectConnectionResolver( $source, $args, $context, $info, $post_types );
// Set the argument that will be passed to WP_Query. We want only Posts (of any post type) that are tagged with this Tag's ID
$resolver->set_query_arg( 'tag_id', $source->term_id );
// Return the connection
return $resolver->get_connection();
}
]);
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment