Posts and Pages
Examples
How to retrieve currently queried post for singular pages
When we visit singular pages in WordPress, such as blog posts, static pages, or custom posts, WordPress performs a database query to fetch the
relevant post information. This fetched data is displayed using the template assigned to that post type. When we are designing a template in
Builderius specifically for singular pages, we can access the currently queried post data using the queried_post
GraphQL field.
- Query
- Result
query{
queried_post{
ID
post_name
post_title
post_content
post_excerpt
post_date
post_date_gmt
post_modified
post_modified_gmt
post_status
post_type
post_parent
guid
has_featured_image
featured_image{
alt_text
caption
description
original_image: file_url
medium_image: file_url(size: MEDIUM)
large_image: file_url(size: LARGE)
}
}
}
{
"queried_post": {
"ID": 1,
"post_name": "ciao-mondo",
"post_title": "Lorem ipsum dolor sit amet consectetur.",
"post_content": "<h1>Hello World</h1>",
"post_excerpt": "post excerpt",
"post_date": "2023-07-21 12:13:29",
"post_date_gmt": "2023-07-21 10:13:29",
"post_modified": "2023-09-08 14:42:53",
"post_modified_gmt": "2023-09-08 12:42:53",
"post_status": "publish",
"post_type": "post",
"post_parent": 0,
"guid": "http://builderius.test/?p=1",
"has_featured_image": true,
"featured_image": {
"alt_text": "alt text",
"caption": "caption",
"description": "description",
"original_image": "http://builderius.test/wp-content/uploads/2023/08/7f2fa3fb434377c9e4cbc1c84b8805ba.jpg",
"medium_image": "http://builderius.test/wp-content/uploads/2023/08/7f2fa3fb434377c9e4cbc1c84b8805ba-300x200.jpg",
"large_image": "http://builderius.test/wp-content/uploads/2023/08/7f2fa3fb434377c9e4cbc1c84b8805ba-1024x683.jpg"
}
}
}
How to retrieve any post based on its ID or slug PRO FEATURE
To retrieve data for any post based on its ID or slug (post_name), you should utilize the post
GraphQL field.
- Query
- Result
query{
some_post: post(identifier: "ID", value: 1) {
ID
post_name
post_title
post_content
post_excerpt
post_date
post_date_gmt
post_modified
post_modified_gmt
post_status
post_type
post_parent
guid
has_featured_image
featured_image{
alt_text
caption
description
original_image: file_url
medium_image: file_url(size: MEDIUM)
large_image: file_url(size: LARGE)
}
}
}
{
"some_post": {
"ID": 1,
"post_name": "ciao-mondo",
"post_title": "Lorem ipsum dolor sit amet consectetur.",
"post_content": "<h1>Hello World</h1>",
"post_excerpt": "post excerpt",
"post_date": "2023-07-21 12:13:29",
"post_date_gmt": "2023-07-21 10:13:29",
"post_modified": "2023-09-08 14:42:53",
"post_modified_gmt": "2023-09-08 12:42:53",
"post_status": "publish",
"post_type": "post",
"post_parent": 0,
"guid": "http://builderius.test/?p=1",
"has_featured_image": true,
"featured_image": {
"alt_text": "alt text",
"caption": "caption",
"description": "description",
"original_image": "http://builderius.test/wp-content/uploads/2023/08/7f2fa3fb434377c9e4cbc1c84b8805ba.jpg",
"medium_image": "http://builderius.test/wp-content/uploads/2023/08/7f2fa3fb434377c9e4cbc1c84b8805ba-300x200.jpg",
"large_image": "http://builderius.test/wp-content/uploads/2023/08/7f2fa3fb434377c9e4cbc1c84b8805ba-1024x683.jpg"
}
}
}
identifier
argument can accept "ID" or "slug" or "[[global data variable]]" or "{{local data variable}}".
value
argument is required.
How to retrieve an array of currently queried posts for archive pages PRO FEATURE
When visiting WordPress archive pages, the blog posts index page or search results page, WordPress queries the database to retrieve data for multiple posts. This data is then used to render the page template in HTML.
When creating a Builderius template for archive pages, the blog posts index page, or search results page, you can access the currently queried posts using the queried_posts
GraphQL field.
- Query
- Result
query {
queried_posts {
count
pages
page
nodes {
ID
post_name
post_title
post_content
post_excerpt
post_date
post_date_gmt
post_modified
post_modified_gmt
post_status
post_type
post_parent
guid
has_featured_image
featured_image {
alt_text
caption
description
original_image: file_url
medium_image: file_url(size: MEDIUM)
large_image: file_url(size: LARGE)
}
}
}
}
{
"queried_posts": {
"count": 10,
"pages": 3,
"page": 1,
"nodes": [
{
"ID": 1,
"post_name": "ciao-mondo",
"post_title": "Lorem ipsum dolor sit amet consectetur.",
"post_content": "<h1>Hello World</h1>",
"post_excerpt": "post excerpt",
"post_date": "2023-07-21 12:13:29",
"post_date_gmt": "2023-07-21 10:13:29",
"post_modified": "2023-09-08 14:42:53",
"post_modified_gmt": "2023-09-08 12:42:53",
"post_status": "publish",
"post_type": "post",
"post_parent": 0,
"guid": "http://builderius.test/?p=1",
"has_featured_image": true,
"featured_image": {
"alt_text": "alt text",
"caption": "caption",
"description": "description",
"original_image": "http://builderius.test/wp-content/uploads/2023/08/7f2fa3fb434377c9e4cbc1c84b8805ba.jpg",
"medium_image": "http://builderius.test/wp-content/uploads/2023/08/7f2fa3fb434377c9e4cbc1c84b8805ba-300x200.jpg",
"large_image": "http://builderius.test/wp-content/uploads/2023/08/7f2fa3fb434377c9e4cbc1c84b8805ba-1024x683.jpg"
}
},
{
"ID": 2,
"post_name": "ciao-mondo2",
"post_title": "Lorem ipsum dolor sit amet consectetur.2",
"post_content": "<h1>Hello World</h1>",
"post_excerpt": "post excerpt",
"post_date": "2023-07-21 12:13:29",
"post_date_gmt": "2023-07-21 10:13:29",
"post_modified": "2023-09-08 14:42:53",
"post_modified_gmt": "2023-09-08 12:42:53",
"post_status": "publish",
"post_type": "post",
"post_parent": 0,
"guid": "http://builderius.test/?p=1",
"has_featured_image": true,
"featured_image": {
"alt_text": "alt text",
"caption": "caption",
"description": "description",
"original_image": "http://builderius.test/wp-content/uploads/2023/08/7f2fa3fb434377c9e4cbc1c84b8805ba.jpg",
"medium_image": "http://builderius.test/wp-content/uploads/2023/08/7f2fa3fb434377c9e4cbc1c84b8805ba-300x200.jpg",
"large_image": "http://builderius.test/wp-content/uploads/2023/08/7f2fa3fb434377c9e4cbc1c84b8805ba-1024x683.jpg"
}
}
]
}
}
How to change default sort order for currently queried posts PRO FEATURE
If we want to modify queried posts, we can utilize the optional argument query
for the GraphQL field queried_posts
.
This argument takes a JSON object with the same arguments as WP_Query. For instance, if we want to enable users to change
the sorting order of posts using a $_GET parameter called sort_order
, which can have values 'ASC' or 'DESC', we should
use the superglobal_variable
GraphQL field to retrieve the value of $_GET["sort_order"]. It's a good practice to
provide a fallback in case sort_order
is not specified.
Additionally, if this field is only needed within the posts query and won't be used elsewhere, we can include
private
argument for this field.
- Query
- Result
query {
selected_sort_order: superglobal_variable(variable: "GET", key: "sort_order", fallback: "ASC") @private
queried_posts (
query: {
order: "{{selected_sort_order}}"
}
) {
count
pages
page
nodes {
post_title
post_content
}
}
}
{
"queried_posts": {
"count": 10,
"pages": 3,
"page": 1,
"nodes": [
{
"post_title": "First Post",
"post_content": "<h1>First Post</h1>"
},
{
"post_title": "Second Post",
"post_content": "<h1>Second Post</h1>"
}
]
}
}
How to retrieve pagination for currently queried posts PRO FEATURE
GraphQL field queried_posts
has subfield pagination
. This subfield will return pagination html.
- Query
- Result
query {
queried_posts {
nodes {
post_title
post_content
}
pagination
}
}
{
"queried_posts": {
"nodes": [
0: {
"post_title": "First Post",
"post_content": "<h1>First Post</h1>"
},
1: {
"post_title": "Second Post",
"post_content": "<h1>Second Post</h1>"
},
],
"pagination": '<span aria-current="page" class="page-numbers current">1</span>
<a class="page-numbers" href="/?author=1&paged=2">2</a>
<a class="page-numbers" href="/?author=1&paged=3">3</a>
<a class="next page-numbers" href="/?author=1&paged=2">Next »</a>'
}
}
pagination
is working based on paginate_links WP function.
pagination
subfield of queried_posts
has optional argument query
, we can modify output of pagination
by using it in this way:
query {
queried_posts {
nodes {
post_title
post_content
}
pagination(
query: {
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 retrieve custom query posts data PRO FEATURE
- Query
- Result
query {
posts (
query: {
post_type: "post",
author: 1,
meta_query: {
relation: "AND",
array: [
{
key: "some_key",
value: "some_value"
}
]
},
tax_query: {
relation: "OR",
array: [
{
taxonomy: "first_taxonomy",
field: "slug",
terms: ["some-term", "another-term"]
},
{
taxonomy: "second_taxonomy",
terms: [25, 27, 29]
}
]
}
}
) {
count
pages
page
nodes {
post_title
post_content
}
}
}
{
"posts": {
"count": 10,
"pages": 3,
"page": 1,
"nodes": [
{
"post_title": "First Post",
"post_content": "<h1>First Post</h1>"
},
{
"post_title": "Second Post",
"post_content": "<h1>Second Post</h1>"
}
]
}
}
GraphQL field posts has argument query
which is json object with all arguments same as WP_Query has.
How to retrieve pagination for custom query posts PRO FEATURE
GraphQL field posts
has subfield pagination
. This subfield will return pagination html.
- Query
- Result
query {
current_page: superglobal_variable(variable: "GET", key: "mypage", fallback: 1) @private
posts (
query: {
author: 1,
paged: "{{current_page}}"
}
) {
nodes {
post_title
post_content
},
pagination (
query: {
paged_query_var_name: "mypage"
}
)
}
}
{
"posts": {
"nodes": [
0: {
"post_title": "First Post",
"post_content": "<h1>First Post</h1>"
},
1: {
"post_title": "Second Post",
"post_content": "<h1>Second Post</h1>"
},
],
"pagination": '<span aria-current="page" class="page-numbers current">1</span>
<a class="page-numbers" href="/?author=1&mypage=2">2</a>
<a class="page-numbers" href="/?author=1&mypage=2">2</a>
<a class="page-numbers" href="/?author=1&mypage=3">3</a>
<a class="next page-numbers" href="/?author=1&mypage=2">Next »</a>'
}
}
pagination
is working based on paginate_links WP function.
Subfield pagination
of posts
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 posts
query will identify current page, but we also need to add argument paged
for posts 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"] 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>", fallback: 1) @private
posts (
query: {
author: 1,
paged: "{{current_page}}"
}
) {
nodes {
post_title
post_content
}
pagination(
query: {
paged_query_var_name: <name of $_GET key based on which `posts` 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 retrieve output of any php function with arguments based on every post in queried_posts PRO FEATURE
If we need for example to get output of function get_post_meta
for every post in queried_posts
, we can do it in this way:
- Query
- Result
query {
queried_posts {
nodes {
id: ID
post_title
color: php_function_output (
function: "get_post_meta",
arguments: [
"{{id}}",
"color",
true
]
)
}
}
}
{
"queried_posts": {
"nodes": [
0: {
"id": 1,
"post_title": "First Post",
"color": "blue"
},
1: {
"id": 2,
"post_title": "Second Post",
"color": "yellow"
},
]
}
}
In this example for every post we used local data variable id
- it is ID
field of post but aliased like id
.
::::
How to retrieve child posts of currently queried post PRO FEATURE
- Query
- Result
query {
queried_post {
ID
post_title
child_posts: posts (
query: {
parent: "{{ID}}"
}
) {
nodes {
post_title
}
}
}
}
{
"queried_post": {
"ID": 1
"post_title": "Hello World",
"child_posts": {
"nodes": [
{
"post_title": "Second Post"
},
{
"post_title": "Third Post"
}
]
}
}
}