Hi @simon1402,
The code itself is fine, the problem is almost certainly that it now exists in two places. If it’s still in your functions.php and you’ve also added it as a WPCode snippet, PHP hits the function change_query_date_value twice and throws a fatal “Cannot redeclare” error. WPCode catches that and deactivates the snippet to keep your site up, which is why it appears to work in functions.php but gets flagged in WPCode.
Here’s the same code tidied up, with the braces laid out properly and an extra check so it doesn’t warn when the value key isn’t set:
function change_query_date_value( $query_args, $block_query, $inherited ) {
if ( ! empty( $query_args['meta_query'][0]['value'] ) ) {
if ( 'todays_date' === $query_args['meta_query'][0]['value'] ) {
$query_args['meta_query'][0]['value'] = date( 'Ymd' );
}
}
return $query_args;
}
add_filter( 'aql_query_vars', 'change_query_date_value', 10, 3 );
If it still gets disabled, please enable Error Logging under WPCode > Settings, re-activate the snippet, and send me the error it logs — it names the exact cause and line. Here’s how: https://wpcode.com/docs/using-snippet-error-logging/
Thanks,
Wow, that is really kind of you. Thank you so much!
I don’t think I ever had the code in both functions.php and the wpcode plugin but I have put your new code into wpcode on my staging site to see if it throws any errors.
I have enabled error logging so fingers crossed!
Thanks again,
Simon
Just checked and the error on the old version is as follows:
Undefined array key “value”
The error occurred on line 4 of this snippet’s code (highlighted below).
With line 4 being:
if ( $query_args['meta_query'][0]['value'] == 'todays_date' ) { $query_args['meta_query'][0]['value'] = date('Ymd'); }
Hi @simon1402,
Thanks for checking that, it confirms the cause.
That first meta_query entry doesn’t always contain a “value” key, so PHP throws a warning and WPCode disables the snippet to be safe. The updated snippet I sent already accounts for this, it checks that the key exists before comparing it, so the warning never fires.
Could you give that version a try and let me know if it stays active and your future events still filter correctly?
Thanks,
That is working brilliantly. Thank you so much!
-
This reply was modified 1 day, 6 hours ago by
simon1402.