Creating Related Posts with Query Loop Block

In GenerateBlocks 1.5 we introduced two new blocks: Query Loop and Image. In addition to that, we also added Dynamic Data options to the Container, Headline, Buttons, and Image blocks. With this update, users were given all the necessary blocks and dynamic data options to build all kinds of dynamic content, including custom query loops.

Now, in GenerateBlocks Pro 1.3 we have added new dynamic parameter options to the already super powerful Query Loop block. With these new options, users will be able to build some of the most commonly requested related post layouts. So if you’re a Pro user, get comfortable as we walk you through how to display related posts.

GB Query Loop Parameters

Before we dive into the new parameter options available in GenerateBlocks Pro 1.3, let’s take a quick look into how query parameters work within the Query Loop block. When you first add a Query Loop block to the page, you will see the following parameters:

These parameters are there by default and will result in the latest 10 posts being displayed. The Inherit query from template option is OFF by default and should be left as such for the examples in this post. Post Type and Posts per page can be changed, but they cannot be removed for good reason.

Building a custom query loop

To build a custom query requires adding more parameters. To do this we begin by clicking the Add Parameter button.

Doing so will result in a new Select query parameter option being added:

You can now select from a comprehensive list of parameters that anyone familiar with WP Query will recognize. Fortunately, GenerateBlocks does all the code behind the scenes for you. So understanding WP Query isn’t a requirement, although it’s nice to know. You can add a single parameter or repeat the process to add as many parameters as you require to create your query. Here are some basic examples that will be relevant to the new Pro features:

Example 1 – Show posts by a specific author

Example 2 – A list of child pages for a specific parent page

Example 3 – Posts within a specific category

Example 4 – Multiple parameters

All of these examples are fairly common patterns that we see every day on WordPress websites. Because of that, we choose these types of queries as the basis for building lists of related posts with GenerateBlocks Pro 1.3.

Related Posts with dynamic parameters

In its simplest form, related posts are a list of posts displayed within, after, or aside from the current post a visitor is reading. The relationship between those posts and the one currently being read can vary greatly.

At its most complex, the post relationship is built with complex data queries and algorithms that use deeper keyword searches, site visitor trends, and other data lookups. These kinds of related posts can put massive demands on a server, and we want to be clear from the start – this is NOT what GenerateBlocks does.

Instead, we allow users to create simple relationships using the same parameters provided in the examples above. ie. by author, by taxonomy, or by hierarchy. In each of our previous examples, we were very specific about what value our parameters included. eg. the author name, the category term, or the parent page. In order for us to display related posts, we needed to make those parameter values behave dynamically.

And we did this In GenerateBlocks Pro 1.3 with the following parameter values:

Current post
Current post terms
Current post author

Let’s now take a look at how we can use these new values to convert our earlier examples into some commonly seen related posts.

The many ways to display related posts.

Posts by the same author

For multi-author sites, it’s quite common to include at the end of the post, or in a sidebar, a list of other posts by the same author. This parameter set will do exactly that whilst making sure we don’t see a duplicate of the current post.

Index of child pages

For pages and other post types that support hierarchies, displaying a list of child posts on the post parent is a good way to provide users with a handy index. This can be achieved with this super simple parameter set.

NOTE: The Post Type needs to support hierarchies

Related posts by taxonomy

A simple, lightweight, and effective related posts method is to use taxonomies to form the post relationships. And this is probably the most sought-after feature. Try these parameters to display posts that share the same category terms.

Related posts including categories, excluding tags

In this example, we will be showing posts that share the same category terms as the current post, along with posts in the Featured category. But at the same time, we want to exclude any posts that share the same tags. For example, we are reading a post in the category of Electric Cars with the tag Toyota. In our related posts, we expect to see more posts on electric cars but perhaps from a different manufacturer.

Random order of posts

In the above Index pages example, we added the Order by -> Menu Order parameter, which makes absolute sense for hierarchical pages. However when it comes to other kinds of related posts what is the best order to choose from?

By default, if no parameter is set, the “Order By” will be based on the posts published date, which is fine when showing a list of the latest posts. But when it comes to related posts by author or taxonomy terms, we may want to mix it up a little. And with GenerateBlocks Pro 1.3 we have added due to popular request, the Order by -> Random parameter.

Troubleshooting when random isn’t random

When it comes to querying a database, the Random parameter can be one of the most expensive queries to run. Especially on larger sites with bigger post databases. And it is for that reason that some hosts disable it by default on their servers.

Another reason that Random may not actually work is caching. For best performance pages are often cached. Which may include the random list of posts that were generated on first page visit.

So if you do run into an issue where Order By -> Random doesn’t seem to be working, it may be best to check your server and cache settings.

Relationship logic with multiple parameter values

A quick note on the logic when adding multiple values to a parameter. In many of the examples above we have a parameter that has many values. For example:

Taxonomy –> Category –> Current post terms, Featured

In this instance there is more than one category term, the display logic here is based on an OR operator. ie. Show any post that has ANY of the given terms.

In future updates, we will be bringing other logic operators to define other kinds of relationships.

Wrapping up related posts

In this post, we took a quick look at building some common query loop layouts. We introduced the new Pro query parameters. And finally how to use those parameters to turn an ordinary query loop into a dynamic related posts loop.

If you’re new to the GenerateBlocks Query Loop block, then check out the prebuilt Patterns we have in the GenerateBlocks Pattern Library. They’re a great way to jump start your designs!

12 thoughts on “Creating Related Posts with Query Loop Block”

  1. GenerateBlocks is really excellent for creating a block-based fast website. I wish to purchase a GB Pro license in a couple of months. And I’m also curious to see more features in GB and GP.
    I love both.
    Thank you for this!

  2. Great article – I’m trying to create a ‘Block – Content Template’ which uses ‘Related Posts’. I’ve managed to include posts using the ‘Categories – Current Post Terms’. I’ve also ‘excluded the current post’. The problem I’m having is excluding posts that have multiple categories.

  3. Hi, I am a generatepress user with a license for life. I have a query regarding generate blocks:

    I need to be able to sort posts by a meta_key. It only lets me select the typical ones like Title, Id, Date ….

    Is there a function for functions.php to be able to select the custom meta_key?


  4. Hello! When is the public release of Generateblocks PRO 1.3.0 scheduled? I can’t wait!

    Can you tell me where I can download the alpha version?

  5. GenerateBlocks Query Loop is a powerful feature.
    From the release of GB version 1.5 I was able to make;
    1. related posts with category inclusion and displayed under every single blog post
    2. latest posts displayed on the website’s homepage
    3. customized archive pages with related logic
    Plus, its is much more easier to design and customize than the default WP block.
    Much thanks for this.

  6. I am waiting for this option for soo long. I am showing related post in my GeneratePress Premium theme with some additional code that sometimes runs and sometimes break my page. So, I was really in trouble but this feature of Generateblocks really helps me.
    Thanks for the post.


Leave a Comment