🎉 Launch Party!

discount for all licences until Aug 31.

Buy now →

Query loop filters

Meta data is great for filtering posts by specific criteria. Modern Fields lets you do this without writing any code, thanks to the Query Loop block.

Meta data is also great for filtering posts by specific criteria. Modern Fields lets you do this without writing any code, thanks to the Query Loop block.

Query loop filters are only available in the pro versions of Modern Fields.

Filter the query loop block

To get started, you need to assign a field group to the post type you want to display using the query Loop block.

Then, in a page or a template, insert the query loop block. In the inspector, select “Custom” in Query type option.

Scroll down and you’ll see a section named “Modern Fields filters“.

Select the fields and set the rules: the query loop preview should update in real time.

You can combine multiple rules. Posts will only appear if all rules are valid.

Check this video for more information:

Filter the query loop with Modern Fields meta

Filter with code

You can also filter a query loop with a bit of PHP code, with native WordPress hooks like pre_get_posts and query_loop_block_query_vars.

With WordPress, you can either filter the main template WP query, or filter a specific query loop block.

Filter the query loop block

First, you need to get the query loop unique ID. Switch to “Code editor” (3 dots on top-right of the editor).

Search for <!-- wp:query and get the ID in the queryId property. Then, go to your functions.php file and add this snippet:

PHP
functions.php

This will filter the posts where the meta score is below 70.

Filter the main query

And to override the main template query:

PHP
functions.php

In this example, it is the main loop of the blog homepage template that is being overridden.

Read about query vars and the WP Query in the WordPress official documentation.