Skip to main content

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

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

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

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 {
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
}
}
}

How to retrieve pagination for currently queried posts PRO FEATURE

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

query {
queried_posts {
nodes {
post_title
post_content
}
pagination
}
}
info

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. 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 retrieve custom query posts data PRO FEATURE

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
}
}
}
info

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

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. 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 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 {
queried_posts {
nodes {
id: ID
post_title
color: php_function_output (
function: "get_post_meta",
arguments: [
"{{id}}",
"color",
true
]
)
}
}
}
info

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 {
queried_post {
ID
post_title
child_posts: posts (
query: {
parent: "{{ID}}"
}
) {
nodes {
post_title
}
}
}
}