Wordpress get_tags() 不适用于 Wp-graphql 插件

问题描述

我正在尝试使用分页获取标签网址。这是完整的代码。我正在尝试扩展 wp-graPHPhql 功能。这样我就可以得到标签分页标签网址。我有超过 30k 个标签。我能够成功获得分页类别、帖子、作者网址,但不知道为什么这个 get_tags() 在 wp-graphql 中无法正常工作。

//Get all tag urls for sitemap
add_action('graphql_register_types',function () {
    register_graphql_field('RootQuery','getTagUrls',[
        'type' => ['list_of' => 'String'],'args' => [
            'pageNo' => [
                'type' => 'int',],'perPage' => [
                'type' => 'int','description' => __('This function returns tag urls,It takes pageNo and PerPage as optional args.'),'resolve' => function ($source,$args,$context,$info) {
            $tagUrls = array();
            $paged = (isset($args['pageNo'])) ? ($args['pageNo']) : 1;
            $perPage = (isset($args['perPage'])) ? ($args['perPage']) : 10;
            $offset = ($paged - 1) * $perPage;
            $number = $perPage + $offset;
            $tags = get_tags(array(
                'orderby' => 'name','order' => 'ASC','number' => $number,'offset' => $offset,));
            foreach ($tags as $tag) {
                $fullUrl = esc_url(get_category_link($tag->term_id));
                $url = str_replace(home_url(),'',$fullUrl);
                array_push($tagUrls,$url);
            }
            return array_merge($tagUrls);
        },]);
});

解决方法

您不需要注册自己的类型来获取标签网址和分页。

在查询 tags(first: 3) 中设置每页的项目数。在这种情况下 3.

query get_links_to_tags {
  tags(first: 3) {
    nodes {
      uri
      link
    }
    pageInfo {
      endCursor
      hasNextPage
      hasPreviousPage
      startCursor
    }
  }
}

在响应中,endCursor 将是最后一项。

{
  "data": {
    "tags": {
      "nodes": [
        {
          "uri": "/tag/apple/","link": "https://test.local/tag/apple/"
        },{
          "uri": "/tag/atari/","link": "https://test.local/tag/atari/"
        },{
          "uri": "/tag/sinclair/","link": "https://test.local/tag/sinclair/"
        }
      ],"pageInfo": {
        "endCursor": "YXJyYXljb25uZWN0aW9uOjI1MQ==","hasNextPage": true,"hasPreviousPage": false,"startCursor": "YXJyYXljb25uZWN0aW9uOjE3Ng=="
      }
    }
  }
}

在上面的示例中,它是 YXJyYXljb25uZWN0aW9uOjI1MQ==。这是一个 base64 编码的字符串,您可以在 Base64decode.org 上对其进行解码,您会看到它显示 arrayconnection:251

进行下一个查询时,将该游标的值用作 after

query get_links_to_tags {
  tags(first: 3,after: "YXJyYXljb25uZWN0aW9uOjI1MQ==") {
    nodes {
      uri
      link
    }
    pageInfo {
      endCursor
      hasNextPage
      hasPreviousPage
      startCursor
    }
  }
}

这将为您返回接下来的三个项目。

{
  "data": {
    "tags": {
      "nodes": [
        {
          "uri": "/tag/tandy/","link": "https://test.local/tag/tandy/"
        },{
          "uri": "/tag/sun-microsystems/","link": "https://test.local/tag/sun-microsystems/"
        },{
          "uri": "/tag/xerox/","link": "https://test.local/tag/xerox/"
        }
      ],"pageInfo": {
        "endCursor": "YXJyYXljb25uZWN0aW9uOjI1NA==","hasPreviousPage": true,"startCursor": "YXJyYXljb25uZWN0aW9uOjI1Mg=="
      }
    }
  }
}

如您所见,您可以使用 hasNextPagehasPreviousPage 来决定是否为分页放置上一个-下一个按钮。