Warning: urlencode() expects parameter 1 to be string, array given

All we need is an easy explanation of the problem, so here it is.

I have posted a question on StackOverflow (sorry for the cross link) and while I’m striving to find a solution to this, I stumbled across this warning :

Warning: urlencode() expects parameter 1 to be string, array given in
/var/www/…/httpdocs/wp-includes/formatting.php on line 3192

And doing a backtrace, I find it originates in query.php :

wp_includes/query.php, lines 1710…1712

if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) {
     $q[$t->query_var] = wp_basename( $q[$t->query_var] );
}

Now, I get this error because one of my URL param is an array of categories, where I’m trying to search and return all product (custom post type) that has one or more of the defined categories assigned.

Why is WordPress trying to call basename on an array of GET parameter is beyond me. Anyone care to explain this to me? Thanks!

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

I’m not sure exactly what’s happening, but I can guess based on a few context clues. That particular function is parse_tax_query. It appears to be checking if the taxonomy is hierarchical, I assume it’s expecting a string that could possibly be a parent/child path of terms, so it uses basename to get the actual term queried- the next line is $term = $q[$t->query_var];

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply