@@ -2714,18 +2714,58 @@ function build_query_vars_from_query_block( $block, $page ) {
27142714 }
27152715 $ query ['tax_query ' ] = array_merge ( $ query ['tax_query ' ], $ tax_query_back_compat );
27162716 }
2717- if ( ! empty ( $ block ->context ['query ' ]['taxQuery ' ] ) ) {
2717+ $ tax_query_input = $ block ->context ['query ' ]['taxQuery ' ];
2718+ if ( ! empty ( $ tax_query_input ) && is_array ( $ tax_query_input ) ) {
27182719 $ tax_query = array ();
2719- foreach ( $ block ->context ['query ' ]['taxQuery ' ] as $ taxonomy => $ terms ) {
2720- if ( is_taxonomy_viewable ( $ taxonomy ) && ! empty ( $ terms ) ) {
2721- $ tax_query [] = array (
2722- 'taxonomy ' => $ taxonomy ,
2723- 'terms ' => array_filter ( array_map ( 'intval ' , $ terms ) ),
2724- 'include_children ' => false ,
2725- );
2720+ // If there are keys other than include/exclude, it's the old
2721+ // format e.g. "taxQuery":{"category":[4]}
2722+ if ( ! empty ( array_diff ( array_keys ( $ tax_query_input ), array ( 'include ' , 'exclude ' ) ) ) ) {
2723+ foreach ( $ block ->context ['query ' ]['taxQuery ' ] as $ taxonomy => $ terms ) {
2724+ if ( is_taxonomy_viewable ( $ taxonomy ) && ! empty ( $ terms ) ) {
2725+ $ tax_query [] = array (
2726+ 'taxonomy ' => $ taxonomy ,
2727+ 'terms ' => array_filter ( array_map ( 'intval ' , $ terms ) ),
2728+ 'include_children ' => false ,
2729+ );
2730+ }
27262731 }
2732+ } else {
2733+ // This is the new format e.g. "taxQuery":{"include":{"category":[4]},"exclude":{"post_tag":[5]}}
2734+
2735+ // Helper function to build tax_query conditions from taxonomy terms.
2736+ $ build_conditions = static function ( $ terms , $ operator = 'IN ' ) {
2737+ $ conditions = array ();
2738+ foreach ( $ terms as $ taxonomy => $ terms ) {
2739+ if ( ! empty ( $ terms ) && is_taxonomy_viewable ( $ taxonomy ) ) {
2740+ $ conditions [] = array (
2741+ 'taxonomy ' => $ taxonomy ,
2742+ 'terms ' => array_filter ( array_map ( 'intval ' , $ terms ) ),
2743+ 'operator ' => $ operator ,
2744+ 'include_children ' => false ,
2745+ );
2746+ }
2747+ }
2748+ return $ conditions ;
2749+ };
2750+
2751+ // Separate exclude from include terms.
2752+ $ exclude_terms = isset ( $ tax_query_input ['exclude ' ] ) && is_array ( $ tax_query_input ['exclude ' ] )
2753+ ? $ tax_query_input ['exclude ' ]
2754+ : array ();
2755+ $ include_terms = isset ( $ tax_query_input ['include ' ] ) && is_array ( $ tax_query_input ['include ' ] )
2756+ ? $ tax_query_input ['include ' ]
2757+ : array ();
2758+
2759+ $ tax_query = array_merge (
2760+ $ build_conditions ( $ include_terms ),
2761+ $ build_conditions ( $ exclude_terms , 'NOT IN ' )
2762+ );
2763+ }
2764+
2765+ if ( ! empty ( $ tax_query ) ) {
2766+ // Merge with any existing `tax_query` conditions.
2767+ $ query ['tax_query ' ] = array_merge ( $ query ['tax_query ' ], $ tax_query );
27272768 }
2728- $ query ['tax_query ' ] = array_merge ( $ query ['tax_query ' ], $ tax_query );
27292769 }
27302770 if ( ! empty ( $ block ->context ['query ' ]['format ' ] ) && is_array ( $ block ->context ['query ' ]['format ' ] ) ) {
27312771 $ formats = $ block ->context ['query ' ]['format ' ];
0 commit comments