元查询不像预期的那样工作

时间:2017-10-18 作者:Mariolex

我在元查询方面有点大问题。我设计了一个查询来检索自定义字段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。

2 个回复
最合适的回答,由SO网友:Mariolex 整理而成

经过长时间的调试,我知道发生了什么。

Wordpress似乎没有很好地处理拉丁文标点符号,就像重音元音(á、é、í、ó、ú)和其他字符一样。

使用此方法:Replacing accented characters php 我在将数据发送到MySql之前更改了元音,现在Wordpress显示帖子

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 );
如果要同时查找两者,则可能需要将关系切换到“或”。

结束

相关推荐

如何在POST中插入各种MySQL字段的内容?

我想在帖子中进行类似于MSWORD邮件合并的操作,以插入我的一个数据库中的变量。我有一个包含字段的数据库,例如名称、street\\u address、city、state和zip,以及photo\\u 1。jpg公司因此,帖子可能会这样写道:“姓名住在城市的街道地址,并拍了这张照片:照片1.jpg。有没有简单的方法可以做到这一点?我尝试进行搜索,但只能找到有关插入MySQL数据库的信息,而不是相反的信息。非常感谢。