• Resolved simon1402

    (@simon1402)


    Hello,

    I am using a snippet copied from elsewhere to create a variable “todays_date” to use with AQL to identify future events. The snippet seems to work okay when added to functions.php but, WPCode flags an error and keeps disabling it. I don’t know PHP so I would be very grateful if anybody could diagnose the cause and suggest a solution. The code is:

    function change_query_date_value( $query_args, $block_query, $inherited ) {
    if ( isset( $query_args[‘meta_query’] ) && ! empty( $query_args[‘meta_query’] ) ) {

        if ( $query_args['meta_query'][0]['value'] == 'todays_date' ) { $query_args['meta_query'][0]['value'] = date('Ymd'); }
    
    }
    return $query_args;

    }
    add_filter( ‘aql_query_vars’, ‘change_query_date_value’, 10, 3 );

    Many thanks,
    Simon

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support markomiljanovic

    (@markomiljanovic)

    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,

    Thread Starter simon1402

    (@simon1402)

    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

    Thread Starter simon1402

    (@simon1402)

    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'); }
    Plugin Support markomiljanovic

    (@markomiljanovic)

    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,

    Thread Starter simon1402

    (@simon1402)

    That is working brilliantly. Thank you so much!

    • This reply was modified 1 day, 6 hours ago by simon1402.
Viewing 5 replies - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.