Support

Account

Home Forums Front-end Issues Creating wp archive using custom field filter and PHP7

Solved

Creating wp archive using custom field filter and PHP7

  • Hey community,

    I have tried this tutorial with select-fields and it works fine in PHP5.6:
    http://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/

    But when I switch to PHP7 I get the following error message:
    ( ! ) Fatal error: Uncaught Error: [] operator not supported for strings in /app/public/wp-content/themes/test/functions.php on line 788

    It refers to this section:

    
    $meta_query[] = array(
    	'key'		=> $name,
    	'value'		=> $value,
    	'compare'	=> 'IN',
    );
    

    It seems that PHP7 doesn’t tolerate the brackets, but unfortunately I have no idea how to change that.
    Does anyone have any idea why?

    Best
    Matthias

  • Is $meta_query already defined before this line?

    If no try

    
    $meta_query = array(
      array(
    	'key'		=> $name,
    	'value'		=> $value,
    	'compare'	=> 'IN',
      )
    );
    
  • Thanks John. This was very helpful. Now I get related cpt posts without Errors or Warnings.

    What do you mean by that?

    Is $meta_query already defined before this line?

    Should I have defined this more clearly?

    // get meta query
     $meta_query = $query->get('meta_query');
  • When you do something like this

    
    $array[] = 'value';
    

    You are adding a value to an array. This assumes that the array already exists. PHP would go ahead and create the array. PHP7.2 is a little more strict, at least I think that is what was happening. So what you need to do is

    
    $array = array();
    $array[] = 'value';
    

    Or in the example I gave above, create the array with the first value at the same time

    
    $array = array('value');
    
  • John, thank you very much for your explanation!

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.