I think this is what you want:
if ($_POST['plaats'] != '') {
$meta_query = array(
'relation' => 'OR',
array(
'key' => 'city',
'value' => $_POST['plaats'],
'compare' => 'LIKE'
),
array(
'key' => 'city2',
'value' => $_POST['plaats'],
'compare' => 'LIKE'
)
);
}
Hi there,
Thanks very much for your response and help!
When I entered the script however and tried to use the search function (frontend) all the entries in de database were echoed. Do you by any chance know if the script as you suggested it works like
SELECT * FROM ... WHERE value1 = $var1 AND value2 = $var2 OR value3 = $var3
or
SELECT * FROM ... WHERE value1 = $var1 AND (value2 = $var2 OR value3 = $var3)
Cheers!
Did you put $meta_query into your $args array? Like this:
if ($_POST['plaats'] != '') {
$meta_query = array(
'relation' => 'OR',
array(
'key' => 'city',
'value' => $_POST['plaats'],
'compare' => 'LIKE'
),
array(
'key' => 'city2',
'value' => $_POST['plaats'],
'compare' => 'LIKE'
)
);
}
$args['meta_query'] = $meta_query;
query_posts($args);
Hi there,
Yes I did. I managed to find out what the array is:
Array ( [0] => Array ( [relation] => OR [0] => Array ( [key] => city [value] => VALUE [compare] => LIKE ) [1] => Array ( [key] => city2 [value] => VALUE [compare] => LIKE ) ) )
VALUE being the value the frontend users submit when using the form
But as mentioned it now echoes all the entries in the database, and I don’t know how to fix it :’-(
Getting all entries indicates that the argument array to the query is not correct. Please print a few lines from your code, starting where you set the arguments array and ending with your query_posts() call.
Please find the code below:
if ($_POST['achternaam'] != '') { $this->input_name = $_POST['achternaam']; }
if ($_POST['provincie'] != '' && $_POST['provincie'] != 'Alle provincies') { $meta_query[] = array('key' => 'provincie', 'value' => $_POST['provincie'], 'compare' => 'LIKE'); }
if ($_POST['modaliteit'] != '') { $meta_query[] = array('key' => 'modality', 'value' => $_POST['modaliteit'], 'compare' => 'LIKE'); }
if ($_POST['plaats'] != '') {
$meta_query[] = array('relation' => 'OR',
array('key' => 'city', 'value' => $_POST['plaats'], 'compare' => 'LIKE'),
array('key' => 'city2', 'value' => $_POST['plaats'], 'compare' => 'LIKE')
);
}
$args = array(
'role' => 'Therapeut',
'posts_per_page' => 1000,
'exclude' => array(1),
'meta_query' => $meta_query
);
BTW: achternaam is Dutch for last name, provincie for province, modaliteit for modality and plaats for city
You have the $meta_query array nested one level too deep. Try changing this:
$meta_query[] = array('relation' => 'OR',
to this:
$meta_query = array('relation' => 'OR',
I tried it and now nothing gets echoed…
OK. Please dump out the $args array like this:
print_r('<pre>ARGS: ');print_r($args);print_r('</pre>');
and post the output.
Hi there!
When I keep the []’s in place the output is:
ARGS: Array
(
[role] => Therapeut
[posts_per_page] => 1000
[exclude] => Array
(
[0] => 1
)
[meta_query] => Array
(
[0] => Array
(
[relation] => OR
[0] => Array
(
[key] => city
[value] => Amsterdam
[compare] => LIKE
)
[1] => Array
(
[key] => city2
[value] => Amsterdam
[compare] => LIKE
)
)
)
)
When I remove the []’s no new output shows up
Is ‘role’ a taxonomy? If so, try using ‘therapeut’ instead of ‘Therapeut’ because the taxonomy slug is what is needed.
Sorry, I did not notice the ‘modaliteit’ part of the $meta_query. Let me try a test and get back to you.
This code should produce the proper $meta_query array:
if ($_POST['achternaam'] != '') { $this->input_name = $_POST['achternaam']; }
if ($_POST['provincie'] != '' && $_POST['provincie'] != 'Alle provincies') { $meta_query[] = array('key' => 'provincie', 'value' => $_POST['provincie'], 'compare' => 'LIKE'); }
if ($_POST['modaliteit'] != '') { $meta_query[] = array('key' => 'modality', 'value' => $_POST['modaliteit'], 'compare' => 'LIKE'); }
if ($_POST['plaats'] != '') {
$meta_query['relation'] = 'OR';
$meta_query[] = array('key' => 'city', 'value' => $_POST['plaats'], 'compare' => 'LIKE');
$meta_query[] = array('key' => 'city2', 'value' => $_POST['plaats'], 'compare' => 'LIKE');
}
$args = array(
'role' => 'therapeut',
'posts_per_page' => 1000,
'exclude' => array(1),
'meta_query' => $meta_query
);
However, I think it will produce unexpected results if both ‘plaats’ and ‘modaliteit’ are present. The ‘OR’ condition will apply to all three keys, not just the two ‘city’ keys. There is currently no way to have the conditional apply to just a subset of the keys.
Of course, this may not be a problem unless ‘modaliteit’ is required along with one of the two city keys.
I think you can use filters to do what you want. First, add these two filters to your functions.php:
function mam_posts_join ($join) {
global $mam_global_join;
if ($mam_global_join) $join .= " $mam_global_join";
return $join;
}
function mam_posts_where ($where) {
global $mam_global_where;
if ($mam_global_where) $where .= " $mam_global_where";
return $where;
}
add_filter('posts_join','mam_posts_join');
add_filter('posts_where','mam_posts_where');
Then, use the code below to create the $args array for your query:
if ($_POST['achternaam'] != '') { $this->input_name = $_POST['achternaam']; }
if ($_POST['provincie'] != '' && $_POST['provincie'] != 'Alle provincies') { $meta_query[] = array('key' => 'provincie', 'value' => $_POST['provincie'], 'compare' => 'LIKE'); }
if ($_POST['plaats'] != '') {
$meta_query['relation'] = 'OR';
$meta_query[] = array('key' => 'city', 'value' => $_POST['plaats'], 'compare' => 'LIKE');
$meta_query[] = array('key' => 'city2', 'value' => $_POST['plaats'], 'compare' => 'LIKE');
}
if ($_POST['modaliteit'] != '') {
$mam_global_join = " JOIN $wpdb->postmeta pm3 ON (pm3.post_id = $wpdb->posts.ID)";
$mam_global_where = " AND pm3.meta_key = 'modality' AND pm3.meta_value = '{$_POST['modaliteit']}'";
}
$args = array(
'role' => 'Therapeut',
'posts_per_page' => 1000,
'exclude' => array(1),
'meta_query' => $meta_query
);