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 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!