msaari / relevanssi

Relevanssi, a WordPress plugin to improve the search
GNU General Public License v3.0
49 stars 21 forks source link

PHP8.1 and relevanssi_meta_query_from_query_vars() #54

Closed chrstffr closed 1 year ago

chrstffr commented 1 year ago

With PHP8.1 I get an error in relevanssi_meta_query_from_query_vars()

On line 1226 in lib/search.php you set $meta_query to boolean false, instead of an empty array; I have queries that passes through without populating $meta_query at all, thus generating the following error:

[12-Sep-2023 12:58:11 UTC] PHP Fatal error: Uncaught ErrorException: Automatic conversion of false to array is deprecated in /<path occluded>/plugins/relevanssi-premium/lib/search.php:1280

https://github.com/msaari/relevanssi/blob/8edf8621ba51aa5ac807f33201050f8c5a19c3ae/lib/search.php#L1226C22-L1226C22

msaari commented 1 year ago

That's a weird issue. The $meta_query variable is supposed to be false if there's no meta query. I don't know why you get that error – I don't see where it's being converted into an array. The function just returns the value, and the function's return type is not specified because it can be either array or bool. Why PHP tries to convert the value to an array?

Can you provide some context where this error happens? I'm not seeing it on my end, I've never seen this error message even though I use PHP 8 (8.2), and I've run hundreds of queries that don't have a meta query without any fatal errors. As far as I can tell, there's nothing wrong here.

chrstffr commented 1 year ago

Well, my query is hitting this line

if ( ! empty( $query->query_vars['meta_key'] ) || ! empty( $query->query_vars['meta_value'] ) || ! empty( $query->query_vars['meta_value_num'] ) ) {

And at that point $meta_query is still a boolean. Then on line 1277 you try to convert it to an array using $meta_query[], can't do that in PHP8.1 if it's a boolean.

Just adding $meta_query = array(); first on line 1276 would solve the problem, or checking if it's a bool there, before adding to it.

msaari commented 1 year ago

Can you please show me the full set of query variables that lead to this case? You have meta_key, meta_value or meta_value_num set without it being a meta query? Just initializing the $meta_query to an array is too simple a fix; I'd like to know what the actual problem here is.

msaari commented 1 year ago

Fixed in 4.21.2.

chrstffr commented 1 year ago

Sorry if I kept you wondering yesterday, had to bounce. When orderby the query result by meta_value or meta_value_num, the meta_key needs to be present in the query, despite it not being a meta query. Example below.

Array
(
    [post_type] => event
    [post_status] => publish
    [posts_per_page] => 16
    [meta_key] => _event_start
    [orderby] => meta_value_num
    [order] => asc
    [s] => test
)

Thanks! 👍