Users
Examples
How to retrieve current user data
- Query
- Result
query {
current_user {
ID
nickname
user_email
user_login
description
roles
}
}
{
"current_user":{
"ID":1,
"nickname":"admin",
"user_email":"[email protected]",
"user_login":"admin",
"description":" Admin user",
"roles":[
0:"administrator"
]
}
}
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
- Result
query {
queried_user {
ID
nickname
user_email
user_login
description
roles
}
}
{
"queried_user":{
"ID":1,
"nickname":"admin",
"user_email":"[email protected]",
"user_login":"admin",
"description":" Admin user",
"roles":[
0:"administrator"
]
}
}
How to get queried post author data
- Query
- Result
query {
queried_post {
ID
post_title
post_content
post_author {
ID
nickname
user_email
user_login
description
roles
}
}
}
{
"queried_post": {
"post_title": "Hello World",
"post_content": "<h1>Hello World</h1>",
"post_author": {
"ID":1,
"nickname":"admin",
"user_email":"[email protected]",
"user_login":"admin",
"description":" Admin user",
"roles":[
0:"administrator"
]
}
}
}
How to get any user data PRO FEATURE
- Query
- Result
query {
some_user: user(identifier: "ID", value: 1) {
ID
post_title
post_content
post_author {
ID
nickname
user_email
user_login
description
roles
}
}
{
"some_user":{
"ID":1,
"nickname":"admin",
"user_email":"[email protected]",
"user_login":"admin",
"description":" Admin user",
"roles":[
0:"administrator"
]
}
}
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
- Result
query {
users (
query: {
role: "administrator",
meta_query: {
relation: "AND",
array: [
{
key: "some_key",
value: "some_value"
}
]
}
}
) {
count
pages
page
nodes {
ID
nickname
}
}
}
{
"users": {
"count": 10,
"pages": 3,
"page": 1,
"nodes": [
{
"ID": 1,
"nickname": "admin1"
},
{
"ID": 2,
"nickname": "admin2"
}
]
}
}
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
- Result
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"
}
)
}
}
{
"users": {
"nodes": [
{
"nickname": "employee1",
"first_name": "John",
"last_name": "Doe"
},
{
"nickname": "employee2",
"first_name": "Peter",
"last_name": "Parker"
}
],
"pagination": '<span aria-current="page" class="page-numbers current">1</span>
<a class="page-numbers" href="/?author=1&emp_page=2">2</a>
<a class="page-numbers" href="/?author=1&emp_page=3">3</a>
<a class="next page-numbers" href="/?author=1&emp_page=2">Next »</a>'
}
}
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. 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 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
- Result
query {
users (
query: {
role: "employee"
}
) {
nodes {
id: ID
nickname
color: php_function_output (
function: "get_user_meta",
arguments: [
"{{id}}",
"color",
true
]
)
}
}
}
{
"users": {
"nodes": [
{
"id": 1,
"nickname": "employee1",
"color": "blue"
},
{
"id": 2,
"nickname": "employee2",
"color": "yellow"
}
]
}
}
In this example for every user we used local data variable id
- it is ID
field of user but aliased like id
.
::::