Forums › Forums › Search & Filter Pro › Invalid argument supplied for foreach() in
- This topic has 10 replies, 3 voices, and was last updated 8 years, 1 month ago by Anonymous.
-
Anonymous(Private) October 2, 2016 at 1:15 pm #61395
I recently upgraded to the latest version and now it is calling an error. Any ideas?
function filter_input_object($input_object, $sfid) { if(($input_object['name'] == ('_sfm_workshop_location' || '_sfm_tour_style' || '_sfm_tour_leader'))) { foreach($input_object['options'] as $option) { if ($option->value !== "") { $option->label = get_the_title($option->value); } } return $input_object; } else { return $input_object; } } add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);
Ross Moderator(Private) October 2, 2016 at 5:29 pm #61397Hi Jacob
I’ll need a few more details.
1) Which version did you upgrade from/to?
2) What is the error message you are getting?
3) Do you see any issues when using your search forms?Thanks
Ross Moderator(Private) October 2, 2016 at 7:30 pm #61412Hey Jacob
I guess this means
$input_object['options']
is not set for one of your fields..To fix this, you would then need to put a test before using that like:
if(isset($input_object['options'])) { foreach($input_object['options'] as $option) {
However, the real issue it seems is why is the
options
array not set for one of your fields.. did you by any chance change the input type of any of these 3 fields:_sfm_workshop_location
,_sfm_tour_style
,_sfm_tour_leader
?
Thanks
Anonymous(Private) October 2, 2016 at 8:24 pm #61414I ran through all the fields and compared them to the original settings that worked before and they were identical. For now using the snippet you posts above seems to have fixed the problem.
p.s on a different note, before filtering, _sfm_workshop_location returns the page IDs for a CPT. Is there anyway I can display only the parent pages?
Ross Moderator(Private) October 3, 2016 at 5:04 pm #61631Ahhh I had an idea…
If
options
was empty for any of those fields, you would literally get a field with no options on the front end.Perhaps its something to do with a secondary search form you have? Because you can also test for
$sfid==136
for example to ensure the fields you are filtering belong to a specific form only.Anyway, you second question, so you wish to remove child pages from the field?
Then you would do something similar as to above, so instead of converting the ID to post title, you will find and check to see if it has a parent ID, something like:
//https://codex.wordpress.org/Function_Reference/wp_get_post_parent_id $parent_id = wp_get_post_parent_id($option->value); if($parent_id) { //then this option has a parent, so it must be a child }
Taking it further, what you would do is create an empty array of options, then only add the options which don’t have parents to the new array…
Then finally, you replace
options
with the new array, only containing parents.Something like this could work although I didn’t test it –
function filter_input_object($input_object, $sfid) { if($input_object['name'] == ('_sfm_workshop_location' || '_sfm_tour_style' || '_sfm_tour_leader')) { $new_options = array(); //the options added to this will replace all existing optoins in the field foreach($input_object['options'] as $option) { if ($option->value !== "") { //check to see if the option has a parent $parent_id = wp_get_post_parent_id($option->value); if(!$parent_id) { //then this option does not have a parent, so it must be a parent itself, add it to the new array $option->label = get_the_title($option->value); array_push($new_options, $option); } } } //now we have an array with only parent options in, so simply replace the one in the input object $input_object['options'] = $new_options; } return $input_object; }
Anonymous(Private) October 4, 2016 at 8:12 am #61768Perfect, the last bit worked exactly as I wanted ๐
Also I have found that the issue with the error at the start is to do with the price slider which makes no sense as that isn’t involved in this function?
Anyway I have removed it for now, but it may be a weird bug I guess ๐
Ross Moderator(Private) October 4, 2016 at 7:15 pm #61997Good stuff ๐
RE your error, if your
if
statement works correctly, then surely the only possible fields at the point of theforeach
loop could be one of those 3 fields, if its to do with the price, then something has gone wrong.After looking again, it looks like the
if
statement is actually not correct:if($input_object['name'] == ('_sfm_workshop_location' || '_sfm_tour_style' || '_sfm_tour_leader')) {
I’m pretty sure in PHP you can’t do that, you have to format it this way:
if(($input_object['name'] == '_sfm_workshop_location') || ($input_object['name'] == '_sfm_tour_style') || ($input_object['name'] == '_sfm_tour_leader')) {
๐
-
AuthorPosts