Skip Navigation

[Resolved] Illegal string offset 'key'

This support ticket is created 3 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 8 replies, has 2 voices.

Last updated by Puntorosso 3 years, 2 months ago.

Assisted by: Shane.

Author
Posts
#2199625

Hi, I use this code for a view, which shows all actual WooCommerce orders.

add_filter( 'wpv_filter_query', 'func_filter_custom_date_with_fulldaytime1', 99, 3 );
function func_filter_custom_date_with_fulldaytime1( $query_args, $settings, $view_id ) {   
    if ( $view_id == 11983) {
          if ( (isset($query_args['meta_query'])) && (!empty($query_args['meta_query'])) ) {  
           $target_field = "wpcf-booking-datetime"; // change this field slug to your field slug
              
             foreach ($query_args['meta_query'] as $key => $value):
                                       if ($value['key'] == $target_field){                                                                                    
                                              $day_start_time =   $value['value'];
                                              $day_end_time = strtotime('+1 day', $day_start_time) - 1;
                                                              $filer_index = $key;                                         
                 }
            endforeach;
                
            if ( isset($filer_index) ) {
                   
                $query_args['meta_query'][$filer_index] = array('key' => $target_field, 
                                                                  'value' => array($day_start_time, $day_end_time), 
                                                                  'type' => 'NUMERIC', 
                                                                  'compare' => 'BETWEEN' );
                                                                  }             
        }   
       
     }
    return $query_args;
}

It works perfectly, but when I check my server logs I see a lot of errors "PHP Warning: Illegal string offset 'key' in ...."

Here is a query URL example we are using.

What am I doing wrong?

Thanks.

#2199969

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

The problem seems to be here
$value['key']

The value variable doesn't have a key attribute based on the message. Can you perform a var_dump on the $value variable and let me know what is inside that variable.

Thanks,
Shane

#2200109

The foreach goes through all queries parameters and $value contains following values:

array(4) { ["key"]=> string(21) "wpcf-booking-datetime" ["value"]=> string(10) "1634601600" ["type"]=> string(7) "NUMERIC" ["compare"]=> string(1) "=" }
array(4) { ["key"]=> string(19) "wpcf-trial-location" ["value"]=> string(3) "155" ["type"]=> string(7) "NUMERIC" ["compare"]=> string(1) "=" } string(3) "AND"
array(4) { ["key"]=> string(21) "wpcf-booking-datetime" ["value"]=> string(10) "1634601600" ["type"]=> string(7) "NUMERIC" ["compare"]=> string(1) "=" }
array(4) { ["key"]=> string(19) "wpcf-trial-location" ["value"]=> string(3) "155" ["type"]=> string(7) "NUMERIC" ["compare"]=> string(1) "=" } string(3) "AND"

#2200781

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

Well its clear that there is a Key attribute in the array, it appears that we may be looking in the wrong place.

Can you paste the entire error that shows the line number where the fault is being thrown ?

This will help to pinpoint exactly where we should be looking.

Thanks,
Shane

#2200909

[Wed Oct 20 16:24:30.980509 2021] [proxy_fcgi:error] [pid 8977:tid 140711483324160] [client 83.56.162.167:39724] AH01071: Got error 'PHP message: PHP Warning: Illegal string offset 'key' in /home/598272.cloudwaysapps.com/gxmcwmrwaz/public_html/wp-co..............m-functions/puntorossomedia-utility.php on line 605PHP message: PHP Warning: Illegal string offset 'key' in /home/598272.cloudwaysapps.com/gxmcwmrwaz/public_html/wp-content/plugins/puntorosso-custom-functions/puntorossomedia-utility.php on line 605'

[Wed Oct 20 16:24:36.750102 2021] [proxy_fcgi:error] [pid 8977:tid 140711508502272] [client 83.56.162.167:39750] AH01071: Got error 'PHP message: PHP Warning: Illegal string offset 'key' in /home/598272.cloudwaysapps.com/gxmcwmrwaz/public_html/wp-co..............untorossomedia-utility.php on line 605', referer:

#2200997

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

I will need to physically interact with the site to understand what line 605 is saying with the context of the entire file.

Would it be possible to get admin access to the site as well as the link to the page where this code is triggered?

Thanks,
Shane

#2201099

Found the solution.
The error is triggered because $value can return an empty array and $target_field is always a string value in foreach.
In this case we need to prevent the comparison by changing the line

if ( $value['key'] == $target_field){ 

to

if ( isset ( $value['key']  ) && $value['key'] == $target_field){ 

Thanks for your help. Best.

#2201101

Please remove the links in the previous ticket, thanks.

#2201121

My issue is resolved now. Thank you!