Skip to main content

Users

Examples

How to retrieve current user data

query {
current_user {
ID
nickname
user_email
user_login
description
roles
}
}

How to retrieve currently queried user (author of all posts on this page) data for author archive pages PRO FEATURE

When visiting WordPress author archive pages - the subject of the page is user (author of all posts on this page). When creating a Builderius template for author archive pages, you can access the currently queried user (author of all posts on this page) using the queried_user GraphQL field.

query {
queried_user {
ID
nickname
user_email
user_login
description
roles
}
}

How to get queried post author data

query {
queried_post {
ID
post_title
post_content
post_author {
ID
nickname
user_email
user_login
description
roles
}
}
}

How to get any user data PRO FEATURE

query {
some_user: user(identifier: "ID", value: 1) {
ID
post_title
post_content
post_author {
ID
nickname
user_email
user_login
description
roles
}
}
info

identifier argument can accept "ID" or "slug" or "email" or "login" or "[[global data variable]]" or "{{local data variable}}". value argument is required.

How to get custom query users data PRO FEATURE

query {
users (
query: {
role: "administrator",
meta_query: {
relation: "AND",
array: [
{
key: "some_key",
value: "some_value"
}
]
}
}
) {
count
pages
page
nodes {
ID
nickname
}
}
}
info

GraphQL field users has argument query which is json object with all arguments same as WP_User_Query has.

How to get pagination for custom query users PRO FEATURE

GraphQL field users has subfield pagination. This subfield will return pagination html.

query {
employee_page: superglobal_variable(variable: "GET", key: "employee_page", 1) @private
users (
query: {
role: "employee",
paged: "{{employee_page}}"
}
) {
nodes {
nickname
first_name
last_name
},
pagination (
query: {
paged_query_var_name: "employee_page"
}
)
}
}
info

pagination is working based on paginate_links WP function. Subfield pagination of users 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 users query will identify current page, but we also need to add argument paged for users 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 {
current_page: superglobal_variable(variable: "GET", key: "<paged_query_var_name from pagination query>", 1) @private
users (
query: {
role: "employee",
paged: "{{current_page}}"
}
) {
nodes {
first_name
last_name
}
pagination(
query: {
paged_query_var_name: <name of $_GET key based on which `users` 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 user in custom users query PRO FEATURE

If we need for example to get output of function get_user_meta for every user in users, we can do it in this way:

query {
users (
query: {
role: "employee"
}
) {
nodes {
id: ID
nickname
color: php_function_output (
function: "get_user_meta",
arguments: [
"{{id}}",
"color",
true
]
)
}
}
}
info

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