Skip to main content

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 {
queried_term {
term_id
slug
description
}
}

How to get queried post categories data

query {
queried_post {
ID
post_title
post_content
categories {
term_id
slug
}
}
}

How to get any term data PRO FEATURE

query {
some_term: term(identifier: "term_id", value: 1, taxonomy: "category") {
term_id
slug
taxonomy
}
}
info

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 {
terms (
query: {
taxonomy: "some-taxonomy",
meta_query: {
relation: "AND",
array: [
{
key: "some_key",
value: "some_value"
}
]
}
}
) {
count
pages
page
nodes {
term_id
slug
}
}
}
info

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 {
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"
}
)
}
}
info

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. DefaultPrevious'.>
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 {
terms (
query: {
taxonomy: "some-taxonomy"
}
) {
nodes {
id: term_id
slug
color: php_function_output (
function: "get_term_meta",
arguments: [
"{{id}}",
"color",
true
]
)
}
}
}
info

In this example for every term we used local data variable id - it is term_id field of term but aliased like id. ::::