This $_GET['variable']
is an array, as per your URL query string ?variable[]=value1&variable[]=value2
, $_GET['variable'][0]
and $_GET['variable'][1]
should return those 2 key values
Edit – after the discussion – making it dynamic
$meta_query = array();
if ( ! empty( $_GET["variable"] ) ) {
if ( is_array( $_GET["variable"] ) ) {
$meta_query['relation'] = 'OR';
foreach ( $_GET["variable"] as $value ) {
$meta_query[] = array(
'key' => 'my_post_field',
'value' => sanitize_text_field( (string) $value ),
'compare' => '='
);
}
} else {
$meta_query = array(
'key' => 'my_post_field',
'value' => sanitize_text_field( (string) $_GET["variable"] ),
'compare' => '='
);
}
}
$filter = array(
'post_type' => 'my_custom_post_type',
'meta_query' => array(
'relation' => 'OR',
$meta_query
)
);
$posts = new WP_Query( $filter );
I have changed the operator to =
because I was testing it and it worked on my local installation.
meta_compare (string) – Operator to test the ‘meta_value’. Possible
values are ‘=’, ‘!=’, ‘>’, ‘>=’, ‘<‘, ‘<=’, ‘LIKE’, ‘NOT LIKE’, ‘IN’,
‘NOT IN’, ‘BETWEEN’, ‘NOT BETWEEN’, ‘NOT EXISTS’, ‘REGEXP’, ‘NOT
REGEXP’ or ‘RLIKE’. Default value is ‘=’.
— codex