Terms
Examples
How to get queried term data PRO FEATURE
For Builderius templates with type collection
- there is GraphQL field queried_term
.
If in Apply Rules We have selected Category Archive
or Tag Archive
or Custom Taxonomy Archive
- this field is to get data for the queried term (based on chosen applicant).
- Query
- Result
query {
queried_term {
term_id
slug
description
}
}
{
"queried_term":{
"term_id":1,
"slug":"some-term",
"description":"Some Term"
}
}
How to get queried post categories data
- Query
- Result
query {
queried_post {
ID
post_title
post_content
categories {
term_id
slug
}
}
}
{
"queried_post": {
"post_title": "Hello World",
"post_content": "<h1>Hello World</h1>",
"categories": [
0: {
"term_id": 1,
"slug": "uncategorized"
}
]
}
}
How to get any term data PRO FEATURE
- Query
- Result
query {
some_term: term(identifier: "term_id", value: 1, taxonomy: "category") {
term_id
slug
taxonomy
}
}
{
"some_term":{
"term_id":1,
"slug":"uncategorized",
"taxonomy":"category"
}
}
identifier
argument can accept "term_id" or "slug" or "name" or "term_taxonomy_id" or "[[global data variable]]" or "{{local data variable}}".
value
argument is required.
taxonomy
argument is required.
How to get custom query terms data PRO FEATURE
- Query
- Result
query {
terms (
query: {
taxonomy: "some-taxonomy",
meta_query: {
relation: "AND",
array: [
{
key: "some_key",
value: "some_value"
}
]
}
}
) {
count
pages
page
nodes {
term_id
slug
}
}
}
{
"terms": {
"count": 10,
"pages": 3,
"page": 1,
"nodes": [
{
"term_id": 1,
"slug": "first-slug"
},
{
"term_id": 2,
"nickname": "second-slug"
}
]
}
}
GraphQL field terms has argument query
which is json object with all arguments same as WP_Term_Query has.
How to get pagination for custom query terms PRO FEATURE
GraphQL field terms
has subfield pagination
. This subfield will return pagination html.
- Query
- Result
query {
term_page: superglobal_variable(variable: "GET", key: "term_page", 1) @private
terms (
query: {
taxonomy: "some-taxonomy",
number: 10
offset: "{{(term_page - 1) * 10}}"
}
) {
nodes {
term_id
slug
},
pagination (
query: {
paged_query_var_name: "term_page"
}
)
}
}
{
"terms": {
"nodes": [
{
"term_id": 1,
"slug": "first-term"
},
{
"term_id": 2,
"slug": "second-term"
}
],
"pagination": '<span aria-current="page" class="page-numbers current">1</span>
<a class="page-numbers" href="/?author=1&term_page=2">2</a>
<a class="page-numbers" href="/?author=1&term_page=3">3</a>
<a class="next page-numbers" href="/?author=1&term_page=2">Next »</a>'
}
}
pagination
is working based on paginate_links WP function.
Subfield pagination
of terms
has required argument query
, and there is required sub argument paged_query_var_name
.
paged_query_var_name
is name of $_GET key based on which terms
query will identify current page, but we also need to add argument paged
for terms query like in example below to get everything working.
In our query we should use superglobal_variable
GraphQL field to get value of $_GET[<paged_query_var_name from pagination query>] but with fallback.
Also if this field we are not going to use anywhere except in posts query - we can add argument private
to this field.
IMPORTANT - word "paged" can't be used as paged_query_var_name
.
query {
term_page: superglobal_variable(variable: "GET", key: "<paged_query_var_name from pagination query>", 1) @private
terms (
query: {
taxonomy: "some-taxonomy",
number: 10,
offset: "{{(term_page - 1) * 10}}"
}
) {
nodes {
term_id
slug
}
pagination(
query: {
paged_query_var_name: <name of $_GET key based on which `terms` query will identify current page>
show_all: <Whether to show all pages. Default false.>
end_size: <How many numbers on either the start and the end list edges. Default 1.>
mid_size: <How many numbers to either side of the current pages. Default 2.>
prev_next: <Whether to include the previous and next links in the list. Default true.>
prev_text: <The previous page text. Default '« Previous'.>
next_text: <The next page text. Default 'Next »'.>
before_page_number: <A string to appear before the page number. Default value ''>
after_page_number: <A string to append after the page number. Default value ''>
}
)
}
}
How to get output of any php function with arguments based on every term in custom terms query PRO FEATURE
If we need for example to get output of function get_term_meta
for every term in terms
, we can do it in this way:
- Query
- Result
query {
terms (
query: {
taxonomy: "some-taxonomy"
}
) {
nodes {
id: term_id
slug
color: php_function_output (
function: "get_term_meta",
arguments: [
"{{id}}",
"color",
true
]
)
}
}
}
{
"terms": {
"nodes": [
{
"id": 1,
"slug": "first-term",
"color": "blue"
},
{
"id": 2,
"slug": "second-term",
"color": "yellow"
}
]
}
}
In this example for every term we used local data variable id
- it is term_id
field of term but aliased like id
.
::::