我目前正在使用WP 3.1,并试图让WP\\U查询与自定义字段一起工作。我的论点如下:
$args = array(
\'post_type\' => \'post\',
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'name\',
\'value\' => "$s",
\'compare\' => \'LIKE\'
),
array(
\'key\' => \'address\',
\'value\' => "$s",
\'compare\' => \'LIKE\'
)
)
);
基本上搜索自定义字段地址和名称,匹配地址或名称。
我没有得到任何结果,也没有打印我得到的SQL查询:
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
因此,不确定为什么SQL没有显示或,以粗体突出显示,而是显示和。我还尝试更改了从或到和的关系,并且SQL保持不变。
不确定我在遵循以下文档时做错了什么:https://codex.wordpress.org/Class_Reference/WP_Query
还尝试了以下操作:
$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 );
感谢您的帮助
科林