-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
array_merge overwrites $query_args['tax_query'] after it's added in aql_query_vars filter #82
Comments
Thanks for opening this issues. I'll look at it and get some unit tests wrapped around this. |
@robistek looking at your code again, it looks like you are actually replacing the key in the hook by assigning You should check to see that it's defined first but the code below is going to add to the existing function remove_pasta_recipes_from_aql_block( array $query_args, array $block_query, bool $inherited ): array {
$query_args['tax_query'][] =
array(
'taxonomy' => 'recipe_category',
'field' => 'slug',
'terms' => 'pasta',
'operator' => 'NOT IN',
'include_children' => false,
);
return $query_args;
}
add_filter( 'aql_query_vars', 'remove_pasta_recipes_from_aql_block', 10, 3 ); |
Thanks for the reply and the patience, @ryanwelcher. I have modified the code, tested it and written a more complete example. I have updated my filter function to make it more robust - thanks for your input: function remove_pasta_recipes_from_aql_block( array $query_args, array $block_query, bool $inherited ): array {
if ( ! $inherited ) {
$additional_query_args = array(
'taxonomy' => 'recipe_category',
'field' => 'slug',
'terms' => 'pasta',
'operator' => 'NOT IN',
'include_children' => false,
);
// Exclude posts where the taxonomy 'recipe_category' has the slug 'pasta'.
if ( isset( $query_args['tax_query'] ) && is_array( $query_args['tax_query'] ) ) {
$query_args['tax_query'][] = $additional_query_args;
} else {
$query_args['tax_query'] = array(
$additional_query_args,
);
}
}
return $query_args;
}
add_filter( 'aql_query_vars', 'remove_pasta_recipes_from_aql_block', 10, 3 ); When this filter returns During debugging I found out that even if $default_query = array (
'post_type' => 'recipe',
'order' => 'DESC',
'orderby' => 'date',
'tax_query' =>
array (
0 =>
array (
'taxonomy' => 'recipe_ingredient',
'terms' =>
array (
0 => 123,
),
'include_children' => false,
),
),
'offset' => 0,
'posts_per_page' => 4,
'author__in' =>
array (
),
);
$filtered_query_args = array (
'post__not_in' =>
array (
0 => 1000,
),
'tax_query' =>
array (
0 =>
array (
'taxonomy' => 'recipe_category',
'field' => 'slug',
'terms' => 'pasta',
'operator' => 'NOT IN',
'include_children' => false,
),
),
); The result of
Am I using the |
When modifying the
$query_args
using theaql_query_vars
filter, thearray_merge
in thequery_loop_block_query_vars
callback function overwrites thetax_query
in$query_args
. This causes issues when custom tax queries are added via filters, as demonstrated in the code belowThe line for reference:
advanced-query-loop/includes/query-loop.php
Line 77 in 17bf11c
$default_query
contains taxonomy filters set via 'Filters' in the 'Block' settings/tab.In this example the array key
tax_query
replaces the previous one when array_merge is used, losing any previous taxonomies defined in$default_query
.A possible fix could be to use
array_merge_recursive
instead ofarray_merge
The text was updated successfully, but these errors were encountered: