Created
August 3, 2020 16:36
-
-
Save jasonbahl/95f63db2edd7c0ef11c1afc44afc61e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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