Page Siblings Connection

Below is a connection that adds the ability to query page siblings (i.e. pages that share the same parent)

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();

} );

Published by Jason Bahl

Jason is a Principal Software Engineer at WP Engine based in Denver, CO where he maintains WPGraphQL. When he's not writing code or evangelizing about GraphQL to the world, he enjoys escaping from escape rooms, playing soccer, board games and Fortnite.