跳转到主要内容

add edit link to all post types


title: "Add Edit Link to All Post Types" wordpressUri: "/recipes/add-edit-link-to-all-post-types/" wordpressId: "7712" group: "Custom Fields" summary: "The following snippet shows how to add the “Edit” link as a GraphQL field to all post types: add_action( 'graphql_register_types', function() { register_graphql_field( 'ContentNode', 'editLink', [ 'type' => 'String', 'de…"

The following snippet shows how to add the “Edit” link as a GraphQL field to all post types:

add_action( 'graphql_register_types', function() {

	register_graphql_field( 'ContentNode', 'editLink', [
		'type' => 'String',
		'description' => __( 'Link to edit the content', 'your-textdomain' ),
		'resolve' => function( \WPGraphQL\Model\Post $post, $args, $context, $info ) {
		   return get_edit_post_link( $post->databaseId );
		}
	]);

} );

This could then be queried like so:

Screenshot of a GraphQL Query for posts with their editLink

Screenshot of a GraphQL Query for posts with their editLink