Discover how to customize which posts are displayed in a particular query loop block thanks to WordPress filters.
The native WordPress query loop block that allows you to display a list of entries is one of the most powerful ones.
In fact, in this tutorial I review all the options offered in its user interface. Although there are quite a few, it does not reach the degree of customization that can be achieved by modifying the parameters of WP_Query
.
However, the WordPress 6.1 version introduces a PHP filter that allows you to interact directly with the query, so the possibilities are extended almost to infinity.
However, this strategy had the limitation that you could use conditional tags to modify the query of a block, but it does not work in case you use more than one block per page.
In today’s tutorial you will see how you can modify the query of a block individually by combining the filters query_loop_block_query_vars
and pre_render_block
.
Let’s see how:
Steps to customize the query of a particular query loop block
First, add a query loop block and configure the settings to your liking.
1. Examine the block ID
Once you have created the block, go to “code editor” mode and examine the HTML.

Pro tip: use the keyboard shortcut Cmd + Alt + Shift + M
to go faster.

At the height of where you have created the block you will see something similar to:
<!-- wp:query {"queryId":8,"query":{"perPage":3,"pages":0,"offset":0,"postType":"post","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false}} -->
The part you are interested in is queryId
since all content blocks have a numeric identifier. In this case it is 8.
2. Modify the query of the chosen block
Add the following code at the end of functions.php or in your functionality plugin:
To see this and another 965 code snippets of this website, login or subscribe here.
In this code you use the filter pre_render_block
, which allows you to access the attributes of all blocks, together with query_loop_block_query_vars
in which you modify the query to ask it to exclude the entries with the category with IDs 10 and 15 in the block that has the identifier 8.
You can get the category ID by checking the URL when you edit the category or, if you want to make it easier for yourself, get it displayed directly in the WordPress admin area with this tutorial.
But this is just an example, you can make any query modification that the WP_Query
class allows you to do. For example:
- Sort by whatever you want (metadata, randomize…)
- Choose which entries are displayed
- Exclude category(ies)/tag(s)
- Show multiple CPTs
- …
For example, this would be the code you would use if you want the query modification to display the combination of multiple CPTs:
To see this and another 965 code snippets of this website, login or subscribe here.
You can see here that having a filter with access to the query gives you extra flexibility compared to what the user interface offers. And this is just an example of what you can achieve.
You can access any WP_Query
parameter, so the possibilities are huge.
Just keep in mind that since you are using a PHP filter you have the limitation that you will only see the result on the web frontend, the backend will not reflect the changes.
Conclusions
You can see that by combining two filters you can customize the query of a specific WordPress native query loop block to show the entries you want.
Any questions? Let me know in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉