GeneratePress AI is here - Read the release post

Learn GeneratePress

How to

Display Posts with the Query Block

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:

  1. select the Query block
  2. 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:

  1. 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:

  1. Add Parameter > Taxonomies
  2. set the Taxonomy to Category
  3. 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.

  1. Add Parameter > Taxonomies
  2. Set the Taxonomy to Category
  3. choose the Category: Current Post Categories
  4. Add Parameter > Exclude Posts
  5. 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.

  1. Add Parameter > Parent
  2. 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.

  1. Add Parameter > Parent
  2. Choose the Parent: Current Page

Posts from a specific author

Display a list of posts from a specific post author

  1. Add Parameter > Author
  2. 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.

  1. Add Parameter > Authors
  2. Choose the Authors: current author
  3. Add Parameter > Exclude Posts
  4. 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.

  1. Add Parameter > Date
  2. Choose the before option if required
  3. Set the Date
  4. Choose the after option if required
  5. 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 order
  • ASC – ascending order

* Random Order posts not working

Random ordering may not work for a few reasons, with the two most common being:

  1. Caching – Cached content can prevent the order from changing. You may need to contact your cache provider to resolve this.
  2. 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 posts
  • exclude – exclude any sticky posts from the query results
  • ignore – ignore the sticky posts so they don’t show at the top of the feed
  • only – 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.