The Query Block is used to display a list of posts. You can control which posts are shown by setting query parameters in the query block.
In this article, we’ll take a quick look at how to display posts for common use cases on a single post or page.
The basic set up for all examples is:
- select the
Query block
- open the block settings tab in the editor right sidebar
You can read about the Query Block here for more information.
The default list of posts
A default Query Block has only 2 parameters set:
Post Type = Post
Posts per Page = 10
This matches the default main query set by WordPress and will display up to the 10 latest posts ( per page ) in date descending order ( most recent post first ).
List of posts for different post types
The Query Block can display any Post Type that is available on the site and that GenerateBlocks can see. To show posts from the different types:
- set the Post Type Parameter to include any post types
Example – displaying all posts from our Block Post Type:
Missing Post Type
If a Post Type is not listed in the parameter options then see the Missing Post Type troubleshooting guide.
Posts that belong to a specific category
You can display a list of post from specific categories using the taxonomy parameter:
- Add Parameter > Taxonomies
- set the Taxonomy to Category
- choose the Category Terms
This method can be used for alternative taxonomies such as Post Tags
Related posts that belong to the current post categories
The Category terms can be set dynamically to match the current post categories. We can also exclude the current post to avoid duplication. This is a very basic but low overhead method of displaying related posts.
- Add Parameter > Taxonomies
- Set the Taxonomy to Category
- choose the Category:
Current Post Categories - Add Parameter > Exclude Posts
- Exclude Posts > Current Post
Example – displaying block guides that match this guides categories
GeneratePress Elements or Block Theme Template parts
You can use a GeneratePress Element or a block theme template part to hook the query loop block into your content templates, to create a dynamic related post section for all posts.
Child pages belonging to a specific parent
Display a list of child pages related to a specific parent page.
- Add Parameter > Parent
- Choose the Parent Post
Example – displaying all block guides of a specific parent page
Child pages belonging to the current page
The parent page can be set dynamically to match the current page. This method will show any pages that are child of the current page.
- Add Parameter > Parent
- Choose the Parent: Current Page
Posts from a specific author
Display a list of posts from a specific post author
- Add Parameter > Author
- Chose the Author
Posts from the current post author
The author can be set dynamically to match the current post author. This method will show any posts from the author of the current post. We can also exclude the current post to avoid duplication.
- Add Parameter > Authors
- Choose the Authors:
current author - Add Parameter > Exclude Posts
- Exclude Posts > Current Post
List posts from a specific date range
The date parameter can display posts before a certain date, after a certain date or between those two dates.
- Add Parameter > Date
- Choose the
beforeoption if required - Set the Date
- Choose the
afteroption if required - Set the Date
Display posts in a specific order
The order in which posts are displayed can be changed by setting the Order By and the Order parameters.
The Order By parameters include:
-
date– ( default ) in date order -
ID– the post ID -
slug– the post slug -
author– the author name -
last modified date– the modified date -
menu order– the menu order as set in the page parameters -
parent ID– the ID of the pages parent -
random– in a random order*
The Order parameter defines which direction to follow:
DESC– descending orderASC– ascending order
* Random Order posts not working
Random ordering may not work for a few reasons, with the two most common being:
- Caching – Cached content can prevent the order from changing. You may need to contact your cache provider to resolve this.
- Server Restrictions – Some hosts disable random queries because they can put a heavy load on the database. Check with your hosting provider if this is the case.
Sticky Posts
WordPress keeps track of sticky posts by saving their Post IDs in a list and placing them at the top of the main post feed. Since GenerateBlocks follows this default behavior, any Query Block will automatically show sticky posts first when applicable.
This may result in extra posts appearing on a page beyond the set limit.
Setting Sticky Post behaviour
You can adjust how the Query Block handles Sticky posts with the Sticky Post parameter.
include– the default behaviour to include sticky postsexclude– exclude any sticky posts from the query resultsignore– ignore the sticky posts so they don’t show at the top of the feedonly– show on the sticky posts
Multiple Parameters provide more options
In most of the examples given we only add one or two parameters. But there are many more parameter options to choose from and combine into a single query.
For example you could display posts of a particular post type that are within a specific taxonomy and were published between certain dates whilst excluding posts from a particular author.