## php – loop through meta_query relations

Question

I am currently working on a project which uses WP_Query to XML conversion so that you can filter through the XML like you would in a normal query. The data only exists in the XML so there is no database tables and/or data as its form an external party.

The query is like this as you would normally in WP_Query and this works in the XML parser, this would generate an url like
https://example.com/cars/?brand=Renault,Mercedes

Query

                $filters = array( 'relation' => 'OR', array( 'key' => 'brand', 'value' => 'Renault', 'compare' => 'LIKE' ), array( 'key' => 'brand', 'value' => 'Mercedes', 'compare' => 'LIKE' ) );  XML Function which reads through relation and reruns the function this if statement lives in.  if ( array_key_exists( 'relation',$query ) ) {
$new_query =$query;
$relation =$query['relation'];
unset( $new_query['relation'] ); return$this->compare_meta_query( $new_query,$item, \$relation );
}


However if the filtering gets more complex like:
https://example.com/cars/?brand=Renault,Mercedes&fuel=Electric

The query would look like this

                    'relation' => 'OR',
array(
'key' => 'brand',
'value' => 'Renault',
'compare' => 'LIKE'
),
array(
'key' => 'brand',
'value' => 'Mercedes',
'compare' => 'LIKE'
),
array(
'relation' => 'AND',
array(
'key' => 'fuel',
'value' => 'Electric',
'compare' => 'LIKE'
),
)
);


But the function above no longer returns the desired results as it doesn’t look through a multi-dimensional array and doesn’t see the second relation.

How would i go on to solve this or expand this function to support it like WordPres does?
I know it needs to know if it’s multi-dimensional but I lost sight on how to proceed.

0
3 weeks 2021-09-03T04:37:37-05:00 0 Answers 0 views 0