Gato GraphQL + Meta Box 演示
在两个站点之间同步文章(包含 Meta Box 和 Slim SEO 元数据)
使用 Gato GraphQL for WordPress,将包含 Meta Box 数据和 Slim SEO 数据的文章从一个 WordPress 站点同步到另一个站点


我们可以将文章从一个 WordPress 站点同步到另一个站点,包括通过 Meta Box 管理的元数据,以及由 Slim SEO(或其他插件)添加的元数据。
在本演示中,我们将使用 GraphQL 完成以下操作:
- 从源站点获取文章及其所有元数据
- 在目标站点创建新文章或更新现有文章,并从源站点复制文章数据和元数据
此 query 需要:
- 源站点安装 Gato GraphQL + PRO 扩展
- 目标站点安装免费的 Gato GraphQL 插件
- 在目标站点的端点上启用嵌套 Mutation
需要提供以下变量:
postType:要在站点间同步的文章的自定义文章类型postSlug:要在站点间同步的文章的别名(slug)downstreamServerGraphQLEndpointURL:目标 WordPress 站点的 GraphQL 端点 URLusername:用于在目标站点进行身份验证的应用程序密码用户名appPassword:用于在目标站点进行身份验证的应用程序密码update:更新现有文章时为true,创建新文章时为false
更新文章时,上游站点与下游站点之间的公共标识符为文章别名(slug)。
GraphQL query 必须在源站点上执行。
以下是 GraphQL query:
query CheckHasCustomPost($postSlug: String!, $postType: String! = post)
{
customPost(by: { slug: $postSlug }, status: any, customPostTypes: [$postType])
@fail(
message: "There is no post in the upstream site with the provided slug"
data: {
slug: $postSlug
}
)
{
rawTitle
@export(as: "postTitle")
rawContent
@export(as: "postContent")
rawExcerpt
@export(as: "postExcerpt")
metaKeys(filter: { exclude: [
"_thumbnail_id",
"_edit_last",
] })
meta(keys: $__metaKeys)
@export(as: "postMeta")
}
isMissingPostInUpstream: _isNull(value: $__customPost)
@export(as: "isMissingPostInUpstream")
}
query ExportCreateCustomPostOnTargetSiteGraphQLQuery(
$update: Boolean! = false
)
@depends(on: "CheckHasCustomPost")
@skip(if: $isMissingPostInUpstream)
@skip(if: $update)
{
query: _echo(value: """
mutation CreateCustomPost(
$postType: String! = post
$postSlug: String!
$postTitle: String!
$postExcerpt: String!
$postContent: String!
$postMeta: NullableListValueJSONObject!
) {
createCustomPost(input: {
customPostType: $postType
title: $postTitle,
excerpt: $postExcerpt,
slug: $postSlug,
contentAs: { html: $postContent },
status: draft,
meta: $postMeta,
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
customPost {
__typename
...on CustomPost {
customPostType
title
excerpt
slug
content
status
}
}
}
}
"""
)
@export(as: "query")
@remove
}
query ExportUpdateCustomPostOnTargetSiteGraphQLQuery(
$update: Boolean! = false
)
@depends(on: "CheckHasCustomPost")
@skip(if: $isMissingPostInUpstream)
@include(if: $update)
{
query: _echo(value: """
mutation UpdateCustomPost(
$postType: String! = post
$postSlug: String!
$postTitle: String!
$postContent: String!
$postExcerpt: String!
$postMeta: NullableListValueJSONObject!
) {
customPost(by: { slug: $postSlug }, status: any, customPostTypes: [$postType]) {
update(input: {
title: $postTitle,
excerpt: $postExcerpt,
contentAs: { html: $postContent },
meta: $postMeta,
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
customPost {
__typename
...on CustomPost {
customPostType
title
excerpt
slug
content
status
}
}
}
}
}
"""
)
@export(as: "query")
@remove
}
query CreateOrUpdateCustomPostOnTargetSite(
$downstreamServerGraphQLEndpointURL: String!
$postSlug: String!
$username: String!
$appPassword: String!
$postType: String! = post
)
@depends(on: [
"ExportCreateCustomPostOnTargetSiteGraphQLQuery",
"ExportUpdateCustomPostOnTargetSiteGraphQLQuery",
])
@skip(if: $isMissingPostInUpstream)
{
loginCredentials: _sprintf(
string: "%s:%s",
values: [$username, $appPassword]
)
@remove
base64EncodedLoginCredentials: _strBase64Encode(
string: $__loginCredentials
)
@remove
loginCredentialsHeaderValue: _sprintf(
string: "Basic %s",
values: [$__base64EncodedLoginCredentials]
)
@remove
_sendGraphQLHTTPRequest(
input: {
endpoint: $downstreamServerGraphQLEndpointURL,
query: $query,
variables: [
{
name: "postSlug",
value: $postSlug
},
{
name: "postTitle",
value: $postTitle
},
{
name: "postContent",
value: $postContent
},
{
name: "postExcerpt",
value: $postExcerpt
},
{
name: "postMeta",
value: $postMeta
},
{
name: "postType",
value: $postType
}
],
options: {
headers: [
{
name: "Authorization",
value: $__loginCredentialsHeaderValue
}
]
}
}
)
}变量示例如下:
{
"postType": "post",
"postSlug": "hello-world",
"downstreamServerGraphQLEndpointURL": "https://target-site.com/graphql",
"update": false,
"username": "admin",
"appPassword": "{ application password, eg: cNEp BVPy QVxF eVqH lggt BTb4 }"
}