Extending the Query Loop block

If you want to exclude output filters (which would be the normal way to do this) you are asking for a query that retrieves 9 posts and 3 ads (a separate post type or taxonomy?) in a specific order. The query block has limited querying abilities, but those can be extended to the full possibilities of WP_Query with the query_loop_block_query_vars filter. So, let’s look into WP_Query.

With WP_Query you can ask for posts from different post types (or taxonomies), so you could retrieve, for instance, the 12 most recent posts and ads. But you could not specify that you want 9 of the one and 3 from the other. This is because WP_Query itself is a simplified querying system on the database. You can make more complex queries, but you need the wpdb class for that. Also, there is no obvious way to put the results in the right order.

In theory, you could use the filter to retrieve the 9+3 posts with wpdb, put the results in the right order (for instance by storing the order in a metafield, which you then use for the orderby argument), store them in a temporary post type and then make a query for the first twelve posts in that temporary type. In that way you won’t need to post process the results of the query block, but it doesn’t look like a very appealing option.

tech