我正在尝试让搜索查询对这些参数使用“OR”。它目前正在使用“AND”,我不知道如何更改它。
我认为这句话:
$args[\'meta\\u query\']=数组(\'relationship\'=>\'或\');
会的,但那不起作用。
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\'
)
)
);
不幸的是,我的结局总是这样:
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"
当我想要这个的时候:
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"
SO网友:Paul \'Sparrow Hawk\' Biron
您没有提供问题中的代码使用的上下文。是否有以下情况:
$args = array (
\'s\' => \'201701057\',
\'meta_query\' => array (
\'relation\' => \'OR\',
array (
\'key\' => \'property_address\',
...
),
array (
\'key\' => \'property_city\',
...
),
...
),
) ;
$query = new WP_Query ($args) ;
如果是这样,那么您应该能够
get_meta_sql 具体如下:
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) ;
}
注意事项
wpse_get_meta_sql_OR()
上述func为
NOT 用于通用代码。。。因为你可能不想
OR
这个
meta_query
如果除了
\'s\' => \'...\'
参数到WP\\U查询。。。但它肯定会生成所需的SQL
IF \'s\' => \'...\'
是WP\\u查询中除“meta\\u查询”之外的唯一参数。