advanced-query-loop icon indicating copy to clipboard operation
advanced-query-loop copied to clipboard

Allow nested meta_query with relation setting

Open jasalt opened this issue 11 months ago • 2 comments

Example WP_Query args that is not possible to achieve in the current block UI:

[
'post_type' => ['post', 'event'],
'meta_query' => [
	'relation' => 'AND',
	[
		'key' => 'crb_pinned_enabled',
		'value' => 'yes'  // boolean
	],
	[
		'relation' => 'OR',  // <- nested OR relation is not possible to set
		[
			'key' => 'crb_pinned_until',
			'value' => '',  // date, when field not set, keep pinned forever
			'compare' => '='
		],
		[
			'key' => 'crb_pinned_until',
			'value' => date('Y-m-d'), // today's not included
			'compare' => '>',
			'type' => 'DATETIME'
		]
	]
],
]

Allowing even one extra meta query level in UI would probably help in many cases without being too crazy to implement. Compare type used in example is also impossible but created another issue for it https://github.com/ryanwelcher/advanced-query-loop/issues/99.

jasalt avatar Jan 05 '25 14:01 jasalt

This is a great addition but I'm not sure how to address this from a UI standpoint. I'm in the process of moving the existing UI to a better layout that matches the Taxonomy query builder and once that lands I might be able to figure out how to add nested queries.

ryanwelcher avatar Jul 09 '25 21:07 ryanwelcher

@ryanwelcher thank's for considering this and the continued improvements.

I thought about a query builder UI with full PHP level control for some time but more complex security considerations led to a setup where the query logic ("the C part in MVC") is hard-coded into site-specific PHP plugin/theme and gets exposed via Block Bindings API to the editor.

A special block is then able to pick up that binding from block attribute panel and can loop it into view as it wishes (e.g. twig-templating-block). Some sort of middle ground leaving template controllable in the editor, fetch any data and also avoid need to create dozen of custom blocks per project.

Maybe AQL or modified vanilla Query Loop could also have an option to use a Block Bindings returning array of WP_Posts as a "escape hatch" allowing complex filtering and logic with things like ACF/SCF Post Object relations or any other custom data models. It would be useful for Block Binding to provide a schema though.

jasalt avatar Jul 11 '25 19:07 jasalt