【问题标题】:How does one access custom fileds in an ACF relation field in GraphQL?如何在 GraphQL 中访问 ACF 关系字段中的自定义字段?
【发布时间】:2019-05-08 15:45:11
【问题描述】:

我将 Gatsby.js 与 WP API 一起使用。通常,您可以使用 GraphiQL 来处理查询,而自动完成功能将有助于回答此类问题。但我认为要么该实用程序没有考虑到这种初始级别,要么我没有正确使用 graphQL...

  query workInnerPageQuery{
    wordpressPage(id: { eq: "e52ad032-9468-5826-9320-a22da5b1e0c9" }) {
      id
      title
      acf{
        case_studies{ // this is a relation field
          post_title
          post_name
          --- how do you get an ACF field here?
        }
      }
    }
  }

例如,我有一个自定义字段“字幕”。这不起作用...

  acf{
    case_studies{
      post_title
      post_name
      acf{
        subtitle
      }
    }
  }

看到副标题是帖子类型“case_study”的一个acf字段,它是这个特定页面上的一个关系项......如何访问ACF字段中的ACF字段?

还有其他人需要解决这个问题吗?

【问题讨论】:

    标签: wordpress graphql gatsby


    【解决方案1】:

    原来我的谷歌技能很烂。这可以通过插件实现: https://github.com/airesvsg/acf-to-rest-api-recursive

    在您的 wordpress 网站上安装该插件,然后将其添加到您的函数文件中:

    add_filter('acf/rest_api/recursive/types', function( $types ) {
      $types += array(
        'case_studies' => 'case_studies'
      );
        return $types;
    } );
    

    请记住,case_studies 是我的帖子类型。您显然需要将您的添加到数组中。

    那么就可以按预期在graphql中递归访问acf了……

      acf{
        case_studies{
          post_title
          post_name
          acf{
            subtitle
          }
        }
      }
    

    我会删除这篇文章,但我认为将它列在堆栈溢出上很有用。

    【讨论】:

      猜你喜欢
      • 2016-06-08
      • 1970-01-01
      • 2021-02-23
      • 2020-05-22
      • 2017-08-15
      • 2017-11-17
      • 2019-01-21
      • 2019-04-17
      • 2021-03-01
      相关资源
      最近更新 更多