• Is there any way we can register co-authors ‘Authors’ field to wpgraphql?
    I want to show multiple authors for news post type by querying ‘Authors’ field in the post. But currently this field is not showing up in wpgraphql.
    Please help here ASAP!

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • colis

    (@colis)

    Hey @kirtiraj, you need to register a custom connection that will add the ‘authors’ field to the Post object in GraphQL. In the resolve callback you would just pass the result of the get_coauthors function to the resolver query, which is essentially a WP_User_Query.

    
    add_action( 'graphql_register_types', 'register_authors_connection' );
    
    function register_authors_connection() {
        if ( function_exists( 'get_coauthors' ) ) {
            register_graphql_connection(
                [
    		'fromType'           => 'Post',
    		'toType'             => 'User',
    		'fromFieldName'      => 'authors',
    		'connectionTypeName' => 'AuthorsToPostConnection',
    		'resolve'            => function ( Post $source, $args, $context, $info ) {
    			$resolver = new UserConnectionResolver( $source, $args, $context, $info );
    			$coauthor_ids = array_map(
                                function( $coauthor ) {
                                    return $coauthor->ID;
                                },
                                get_coauthors( $source->postId )
                            );
    
    			$resolver->set_query_arg( 'include', $coauthor_ids );
    			return $resolver->get_connection();
                    },
                ]
            );
        }
    }
    
Viewing 1 replies (of 1 total)
  • The topic ‘WPGraphQL support for Co-Authors Plus Plugin’ is closed to new replies.