Invalid argument supplied for foreach() in

Forums › Forums › Search & Filter Pro › Invalid argument supplied for foreach() in

Viewing 10 posts – 1 through 10 (of 11 total)
  • Anonymous
    #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
    #61397

    Hi 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

    Anonymous
    #61399
    This reply has been marked as private.
    Ross Moderator
    #61412

    Hey 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
    #61414

    I 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
    #61631

    Ahhh 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
    #61768

    Perfect, 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
    #61997

    Good stuff 🙂

    RE your error, if your if statement works correctly, then surely the only possible fields at the point of the foreach 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')) {

    🙂

    Anonymous
    #62001

    Bingo 🙂 Thanks!

    Trevor
    #62136

    Is this working now Jacob and can we close the thread?

Viewing 10 posts – 1 through 10 (of 11 total)
Search & Filter Support
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

We also use cookies to store items in your cart as well as allowing your to login on the site.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

By continuing to use this site, you also agree to our Privacy Policy.