Created
July 13, 2023 14:47
-
-
Save jasonbahl/f88020f0d48e785b38ec18c578ea281d to your computer and use it in GitHub Desktop.
Register a page siblings connection in WPGraphQL
This file contains 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' => 'Page', | |
'toType' => 'Page', | |
'connectionTypeName' => 'PageSiblings', | |
'fromFieldName' => 'siblings', | |
'resolve' => function( $page, $args, $context, $info ) { | |
$parent = $page->parentDatabaseId ?? null; | |
if ( ! $parent ) { | |
return null; | |
} | |
$resolver = new \WPGraphQL\Data\Connection\PostObjectConnectionResolver( $page, $args, $context, $info ); | |
$resolver->set_query_arg( 'post_parent', $parent ); | |
$resolver->set_query_arg( 'post__not_in', $page->databaseId ); | |
return $resolver->get_connection(); | |
} | |
]); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment