Created
September 21, 2021 21:51
-
-
Save galengidman/0658a1b025a94e5e2602229303140a82 to your computer and use it in GitHub Desktop.
Merge WP_Query args, recursively for meta_query and tax_query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function merge_wp_query_args(...$sets) | |
{ | |
$meta_query = []; | |
$tax_query = []; | |
foreach ($sets as &$set) { | |
if (array_key_exists('meta_query', $set)) { | |
$meta_query[] = $set['meta_query']; | |
unset($set['meta_query']); | |
} | |
if (array_key_exists('tax_query', $set)) { | |
$tax_query[] = $set['tax_query']; | |
unset($set['tax_query']); | |
} | |
} | |
$args = array_merge(...$sets); | |
if ($meta_query) { | |
$args['meta_query'] = array_merge(...$meta_query); | |
} | |
if ($tax_query) { | |
$args['tax_query'] = array_merge(...$tax_query); | |
} | |
return $args; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment