I am currently using WP 3.1 and trying to get WP_QUERY to work with the custom fields. My argument is as follows:
$args = array(
'post_type' => 'post',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'name',
'value' => "$s",
'compare' => 'LIKE'
),
array(
'key' => 'address',
'value' => "$s",
'compare' => 'LIKE'
)
)
);
Basically searching the custom fields address and name, matching address OR name.
I am not getting any results and printing the SQL query I am getting:
SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts
INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id)
WHERE 1=1 AND wp_posts.post_type = 'post'
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')
AND wp_postmeta.meta_key = 'name'
AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '%tom%'
AND mt1.meta_key = 'address'
AND CAST(mt1.meta_value AS CHAR) LIKE '%tom%'
ORDER BY wp_posts.post_date DESC LIMIT 0, 10
So not sure why the SQL is not showing OR, highlighted in bold, but showing AND. I have also tried changing the relation, from OR to AND and the SQL remains identical.
Not sure what I am doing wrong having followed the documentation located at: https://codex.wordpress.org/Class_Reference/WP_Query
Also tried this:
$meta_query[] = array('key' => 'name','value' => $s ,'compare' => 'LIKE');
$meta_query[] = array('key' => 'address','value' => $s ,'compare' => 'LIKE');
$args = array(
'post_type' => 'post',
'relation' => 'OR',
'meta_query' => $meta_query
);
$the_query = new WP_Query( $args );
Appreciate your help
Colin