我在元查询方面有点大问题。我设计了一个查询来检索自定义字段pickup\\u地址中存在这些单词的帖子。字段的元值中有此字符串:43 Longview Drive,Papamoa
EDIT
这是Im使用的元查询:
$args = array(
\'post_type\' => \'posts\',
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'pickup_address\',
\'value\' => \'Papamoa\',
\'compare\' => \'LIKE\',
),
array(
\'key\' => \'pickup_address\',
\'value\' => \'Bah�a\',
\'compare\' => \'LIKE\',
),
array(
\'key\' => \'pickup_address\',
\'value\' => \'de\',
\'compare\' => \'LIKE\',
),
array(
\'key\' => \'pickup_address\',
\'value\' => \'Plenty\',
\'compare\' => \'LIKE\',
),
),
);
这是我从元查询生成的代码:
SELECT SQL_CALC_FOUND_ROWS wpiw_posts.ID FROM wpiw_posts INNER JOIN
wpiw_postmeta ON ( wpiw_posts.ID = wpiw_postmeta.post_id ) WHERE 1=1
AND ( (
( wpiw_postmeta.meta_key = \'pickup_address\' AND wpiw_postmeta.meta_value LIKE \'%Papamoa%\' ) OR
( wpiw_postmeta.meta_key = \'pickup_address\' AND wpiw_postmeta.meta_value LIKE \'%Bah�a%\' ) OR
( wpiw_postmeta.meta_key = \'pickup_address\' AND wpiw_postmeta.meta_value LIKE \'%de%\' ) OR
( wpiw_postmeta.meta_key = \'pickup_address\' AND wpiw_postmeta.meta_value LIKE \'%Plenty%\' ) ) )
AND wpiw_posts.post_type = \'project\' AND ((wpiw_posts.post_status = \'publish\')) GROUP BY wpiw_posts.ID ORDER BY wpiw_posts.post_date DESC LIMIT 0, 10
我试着替换这个特殊的角色� 但也不起作用
Wordpress在Wp\\u查询($Query\\u args)上没有给我任何信息,但如果我在MySql中运行它,它就会工作。任何帮助都将不胜感激
EDIT 2
我捕获了Wordpress在转换meta\\u查询参数后对MySql执行的查询:
SELECT SQL_CALC_FOUND_ROWS wpiw_posts.ID FROM wpiw_posts
INNER JOIN wpiw_postmeta ON ( wpiw_posts.ID = wpiw_postmeta.post_id )
INNER JOIN wpiw_postmeta AS mt1 ON ( wpiw_posts.ID = mt1.post_id )
WHERE 1=1 AND
( ( ( wpiw_postmeta.meta_key = \'pickup_address\' AND wpiw_postmeta.meta_value LIKE \'%Papamoa%\' )
OR ( wpiw_postmeta.meta_key = \'pickup_address\' AND wpiw_postmeta.meta_value LIKE \'%BahÃa%\' )
OR ( wpiw_postmeta.meta_key = \'pickup_address\' AND wpiw_postmeta.meta_value LIKE \'%de%\' )
OR ( wpiw_postmeta.meta_key = \'pickup_address\' AND wpiw_postmeta.meta_value LIKE \'%Plenty%\' ) )
AND ( mt1.meta_key = \'deliver_address\' AND mt1.meta_value LIKE \'%Auckland%\' ) )
AND wpiw_posts.post_type = \'project\' AND ((wpiw_posts.post_status = \'publish\')) GROUP BY wpiw_posts.ID ORDER BY wpiw_posts.post_date DESC LIMIT 0, 10
我注意到奇怪的chacarters“BahÃa”,但当我接受这个查询并在MySql上运行它时,它就工作了!检索1行,因为一篇文章的pickup\\u地址字段带有单词Papamoa,deliver\\u地址字段带有work Auckland。
SO网友:Tom
马里奥列克斯,
您是否尝试过使用meta\\u查询?我写了一些快速且未经测试的东西,但它可能会起作用:
$args = array(
\'post_type\' => \'posts\',
\'showposts\' => \'-1\',
\'meta_query\' => array(
array(
\'key\' => \'pickup_address\', //meta_key
\'value\' => \'43 Longview Drive, Papamoa\', //meta_value
\'compare\' => \'LIKE\', //compare
),
),
);
$query = new WP_Query( $args );
print_r($query);
如果这不起作用,您可以通过将短语的每个部分分解为meta\\u查询选项的单独数组来重新创建与您的查询类似的查询。我认为你不需要这样做,因为类似的比较应该能找到你想要的东西。
请务必查看WordPress上的元查询文档。org-这是一个功能强大的工具,我在这种情况下一直使用它。
资料来源:
EDIT
我第一次读到你的问题时误解了。您可以尝试一下,看看是否与值“43 Longview Drive,Papamoa”匹配。
$args = array(
\'post_type\' => \'posts\',
\'showposts\' => \'-1\',
\'meta_query\' => array(
\'relation\' => \'AND\',
array(
\'key\' => \'pickup_address\', //meta_key
\'value\' => \'Papamoa\', //meta_value
\'compare\' => \'LIKE\', //compare
),
array(
\'key\' => \'pickup_address\', //meta_key
\'value\' => \'Bah�a\', //meta_value
\'compare\' => \'LIKE\', //compare
),
array(
\'key\' => \'pickup_address\', //meta_key
\'value\' => \'de\', //meta_value
\'compare\' => \'LIKE\', //compare
),
array(
\'key\' => \'pickup_address\', //meta_key
\'value\' => \'Plenty\', //meta_value
\'compare\' => \'LIKE\', //compare
),
),
);
$query = new WP_Query( $args );
如果要同时查找两者,则可能需要将关系切换到“或”。