0

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

0

1 Answer 1

1

The functionality may not have been added until after 3.1.

The Codex refers to the latest version of WordPress. When in doubt, upgrade.

7
  • Okay, I'll upgrade however, with one parameter being passed, results are returned, same with changing address to "NOT LIKE" and its value. So it does seem to work. Anyone else come across this problem?
    – Coliin Liu
    Commented Apr 8, 2013 at 13:56
  • 3.2 is not making a difference. Surely the SQL should not be "AND" but "OR". Located at AND mt1.meta_key = 'address'. Bug?
    – Coliin Liu
    Commented Apr 8, 2013 at 14:57
  • Why not update to 3.5.1? It's difficult to help when you're using such as old version.
    – vancoder
    Commented Apr 8, 2013 at 17:51
  • Can someone verify the code is correct, especially the second one? Using two likes and the "relation" OR is in the correct location? Appreciated
    – Coliin Liu
    Commented Apr 9, 2013 at 15:33
  • See Multiple Custom Field Handling in the codex. The relation element should be inside meta_query.
    – vancoder
    Commented Apr 9, 2013 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.