If you’ve been using WordPress for a while, you’ve probably encountered WordPress post queries. It is also known as The Loop, Post Loop, Post List, Post Query, or WP Query. The WordPress posts query retrieves a list of posts from the WordPress database for display as a list, grid, or similar. We can display posts based on various arguments, such as their category (or taxonomy term), type, and date range. As is often the case, the list of posts may contain 50, 100, or 10000 posts. Because of this, we split the results into pages and show a certain number of posts per page.
You can create such lists of posts and design them with Builderius. We will focus on getting a list of desired posts and adding pagination to them and leave the design of the posts query for a future tutorial.
Click on Settings > Data Variables > Query to DB (GraphQL) > Examples > Posts and Pages
This is the code I have copied
query {
current_page: superglobal_variable(variable: "GET", key: "mypage", fallback: 1, private: true)
posts (
query: {
author: 1,
paged: "{{current_page}}"
}
) {
nodes {
post_title
post_content
..................
},
pagination (
query: {
paged_query_var_name: "mypage"
}
)
}
}
After you paste the same code, you need to edit it so it fits your needs. Editor has autocomplete and autosuggestions to help you on your way. For example I have edited the line 6 to define the post type as posts only.
I have created my structure like this:
section > collection > template > heading (oversiplified structure but works)
To display the title of each post “dynamically” write this “shortcode”
{{{post_title}}}
Now we also need to add pagination:
I have added a RawHTML module and renamed it to “Pagination” for clarity when I view the structure panel. Pagination module has to be directly inside the section but after the Collection, if it was inside the collection it would repeat after each post title.
In this tutorial the datavar we created is called “postsPaginated” so we select that.
Post titles should now appear
Select the RawHTML module in the structure panel, and click on the DB icon.
Congratulations, you have created your post loop. Next step, let’s design it to look like we want.
© 2021. Builderius. All rights reserved