0

I am trying to get the search query to use an 'OR' for these arguments. It is currently using 'AND' which I can not figure out how to change.

I thought the line:

$args['meta_query'] = array('relation' => 'OR');

would do it, but that is not working.

if ($search != '') {    
        $args = array(

            's' => $search,

            'posts_per_page' => $posts_per_page,

            'paged' => $paged,

            'post_type' => 'property',

            'post_status' => 'publish'

        );
    }else{
        $args = array(

            'posts_per_page' => $posts_per_page,

            'paged' => $paged,

            'post_type' => 'property',

            'post_status' => 'publish'

        );
    }


 //Custom Searches

        $search_array = get_search_array($search);
        $compare = 'REGEXP';

        $search_array = implode("|", $search_array);


        $args['meta_query'] = array('relation' => 'OR');


        array_push($args['meta_query'],

            array('relation' => 'OR', 

                array(

                'key'     => 'property_address',

                'value'   => $search_array,

                'compare' => $compare

                ),

                array(

                'key'     => 'property_city',

                'value'   => $search_array,

                'compare' => $compare

                ),

                array(

                'key'     => 'property_state',

                'value'   => $search_array,

                'compare' => $compare

                ),

                array(

                'key'     => 'property_zip',

                'value'   => $search_array,

                'compare' => $compare

                ),

                array(

                'key'     => 'mlnumber',

                'value'   => $search_array,

                'compare' => $compare

                ),

                array(

                'key'     => 'marketing_remarks',

                'value'   => $search,

                'compare' => 'LIKE'

                )

            )

        );

Unfortunately, I keep ending up with this:

SELECT SQL_CALC_FOUND_ROWS  wp_posts.* FROM wp_posts  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1  AND (((wp_posts.post_title LIKE '%201701057%') OR (wp_posts.post_excerpt LIKE '%201701057%') OR (wp_posts.post_content LIKE '%201701057%')))  AND ( 
  ( 
    ( wp_postmeta.meta_key = 'property_address' AND wp_postmeta.meta_value REGEXP '201701057' ) 
    OR 
    ( wp_postmeta.meta_key = 'property_city' AND wp_postmeta.meta_value REGEXP '201701057' ) 
    OR 
    ( wp_postmeta.meta_key = 'property_state' AND wp_postmeta.meta_value REGEXP '201701057' ) 
    OR 
    ( wp_postmeta.meta_key = 'property_zip' AND wp_postmeta.meta_value REGEXP '201701057' ) 
    OR 
    ( wp_postmeta.meta_key = 'mlnumber' AND wp_postmeta.meta_value REGEXP '201701057' ) 
    OR 
    ( wp_postmeta.meta_key = 'marketing_remarks' AND wp_postmeta.meta_value LIKE '%201701057%' )
  )
) AND wp_posts.post_type = 'property' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_title LIKE '%201701057%' DESC, wp_posts.post_date DESC LIMIT 0, 1000"

When I would like this:

SELECT SQL_CALC_FOUND_ROWS  wp_posts.* FROM wp_posts  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1  AND (((wp_posts.post_title LIKE '%201701057%') OR (wp_posts.post_excerpt LIKE '%201701057%') OR (wp_posts.post_content LIKE '%201701057%')))  **OR** ( 
  ( 
    ( wp_postmeta.meta_key = 'property_address' AND wp_postmeta.meta_value REGEXP '201701057' ) 
    OR 
    ( wp_postmeta.meta_key = 'property_city' AND wp_postmeta.meta_value REGEXP '201701057' ) 
    OR 
    ( wp_postmeta.meta_key = 'property_state' AND wp_postmeta.meta_value REGEXP '201701057' ) 
    OR 
    ( wp_postmeta.meta_key = 'property_zip' AND wp_postmeta.meta_value REGEXP '201701057' ) 
    OR 
    ( wp_postmeta.meta_key = 'mlnumber' AND wp_postmeta.meta_value REGEXP '201701057' ) 
    OR 
    ( wp_postmeta.meta_key = 'marketing_remarks' AND wp_postmeta.meta_value LIKE '%201701057%' )
  )
) AND wp_posts.post_type = 'property' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_title LIKE '%201701057%' DESC, wp_posts.post_date DESC LIMIT 0, 1000"
  • Relation is between meta query arguments, you can't OR the whole meta query. – Milo Mar 14 '17 at 0:13
  • OK. But I obviously don't understand where I am doing that or doing it incorrectly. How do I change that 'AND' to an 'OR'? – user7619044 Mar 14 '17 at 0:58
  • That AND is not controlled by meta query arguments, you can't change that without a direct SQL filter. – Milo Mar 14 '17 at 1:01
  • OK got ya. Well, that's no good. – user7619044 Mar 14 '17 at 17:11
0

You don't provide the context of where the code in your question is being used. Is it something like the following:

$args = array (
    's' => '201701057',
    'meta_query' => array (
        'relation' => 'OR',
        array (
            'key' => 'property_address',
            ...
            ),
        array (
            'key' => 'property_city',
            ...
            ),
        ...
        ),
    ) ;
$query = new WP_Query ($args) ;

If so, then you should be able to hook into get_meta_sql as follows:

add_filter ('get_meta_sql', 'wpse_get_meta_sql_OR', 10, 6) ;

function
wpse_get_meta_sql_OR ($sql, $queries, $type, $primary_table, $primary_id_column, $context)
{
    $sql['where'] = preg_replace ('/^(\s*)AND/', "$1OR", $sql['where']) ;

    return ($sql) ;
}

Caveat

As is, the wpse_get_meta_sql_OR() func above is NOT intended to be used in general purpose code...because you MIGHT not want to OR the meta_query if there were other than the 's' => '...' arg to WP_Query...but it certainly produces your desired SQL IF 's' => '...' is the ONLY other arg to WP_Query than the `meta_query'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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