Skip Navigation

[Resolved] PHP Error

This support ticket is created 3 years, 4 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 4 replies, has 3 voices.

Last updated by andrewD-10 3 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#1871017

Tell us what you are trying to do?

Hi, toolset

I have this code for one of my tables that shows the past bid date for all my posts. The Bid date is a custom date field. Upon load and when searching it will show me all past dated posts.

Here is the code:

<?php
/**
* New custom code snippet (replace this with snippet description).
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.
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 == 41104) {
if ( (isset($_GET['wpv-wpcf-bid_date'])) && (!empty($_GET['wpv-wpcf-bid_date'])) ) {
$target_field = "wpcf-bid_date"; // 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' => $day_end_time,
'type' => 'NUMERIC',
'compare' => '<=' );
}
} else {
$bid_field_slug = "wpcf-bid_date";
$timestamp = current_time('timestamp');
$yesterday = strtotime('-12 hours', $timestamp);

$query_args['meta_query'][] = array(
'key' => $bid_field_slug,
'value' => $yesterday,
'type' => 'NUMERIC',
'compare' => '<='
);
}
}
return $query_args;
}

The error I receive is:
PHP Warning: Illegal string offset 'key' in /home/w2nnms2ait2i/public_html/wp-content/toolset-customizations/past-bid-table.php on line 16

Line 16 is: if ($value['key'] == $target_field){

Would you help me in figuring out why this might be happening?

- Andrew

#1871347

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Andrew

Dump $query_args['meta_query'] to your logs to inspect it and you'll see that it is not just arrays of key => value pairs but also includes a ['relation'] element.

Your foreach loop is looping over all of the elements and your code is written as if all of the elements will be arrays of key => value pairs, but that relation element is not, which is why you see the warning.

I think that's the case. You should add a condition inside your foreach loop to check if $value is an array before handling it as such.

#1872109

The code should not be an array, at least I believe it shouldn't be.

However, to solve the issue should I put this code into an array format by replacing:

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

with:

$arr = array($target_field);
if ( in_array($value['key'], $arr)) {

Will this work?
I am not very good with coding so I don't know exactly how to add a condition to check the array.

Thank you,
Andrew

#1872935

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

The issue you are referring to is not PHP error but PHP warning.

What if you try to change the line of code from:

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

To

if (isset($value['key']) and ($value['key'] == $target_field)){
#1876387

Thank you both solutions (making it array & adding "isset") worked perfectly. Thank you both for your help!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.