WP_QUERY返回的SQL具有wp_posts.ID=-1

时间:2013-09-27 作者:user2175705

我有一个基于这些参数的自定义循环(如下所示),由于某种原因,在运行时,返回的SQL包含以下片段:

。。。和wp\\U帖子。ID=-1。。。

我没有从db得到任何结果。如果在没有此片段的情况下对db运行查询,则会得到所需的结果。有人能指出导致这种行为的参数列表有什么问题吗?

$args = array (
    \'post_status\'           => \'draft\',
    \'posts_per_page\'        => 10,
    \'category_name\'         => $language_sql_param, //fixed
    \'meta_query\'            => array(
        array(
            \'key\'       => \'review_rating\',
            \'value\'     => \'3\',
            \'compare\'   => \'>\',
            \'type\'      => \'NUMERIC\',
        ),
    ),
    \'cache_results\'          => false,
    \'update_post_meta_cache\' => false,
    \'update_post_term_cache\' => false,
    \'suppress_filters\' => true,
    \'meta_key\' => \'review_id\',
    \'orderby\'  => \'meta_value_num\',
); 

$wp_query = new WP_Query( $args );
修改后的问题:我正在根据帖子所属的类别筛选帖子,在我的情况下,它们是国家代码。下面是WP在设置$language_sql_param 对此:

$language_sql_param = \'us,gb,ca,au,ie\'
它工作得很好,并为我在英语国家的帖子返回ID。

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
    INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
    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_term_relationships.term_taxonomy_id IN (20,21,28,36,37) )
        AND wp_posts.post_type = \'post\'
        AND (wp_posts.post_status = \'draft\')
        AND (wp_postmeta.meta_key = \'review_id\'
        AND (mt1.meta_key = \'review_rating\'
        AND CAST(mt1.meta_value AS SIGNED) > \'3\') )
GROUP BY wp_posts.ID
ORDER BY wp_postmeta.meta_value+0 DESC
LIMIT 0, 10
当我设置$language_sql_param = "\'de,ch\'", 我得到以下SQL:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
    INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
    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.ID = -1
        AND ( wp_term_relationships.term_taxonomy_id IN (27,34) )
        AND wp_posts.post_type = \'post\'
        AND (wp_posts.post_status = \'draft\')
        AND (wp_postmeta.meta_key = \'review_id\'
        AND (mt1.meta_key = \'review_rating\'
        AND CAST(mt1.meta_value AS SIGNED) > \'3\') )
GROUP BY wp_posts.ID
ORDER BY wp_postmeta.meta_value+0 DESC
LIMIT 0, 10
SQL有这个额外的AND wp_posts.ID = -1. WordPress似乎正确地为我正在筛选的类别生成IN子句,如AND (wp_term_relationships.term_taxonomy_id IN (27,34)), 但因为WordPress不知何故添加了AND wp_posts.ID = -1 条件没有返回帖子。

在哪里AND wp_posts.ID = -1 来自哪里?

2 个回复
SO网友:Charles Clarkson

如果使用语法突出显示编辑器,则可以更容易地发现这些问题。

更改此代码:

\'category_name\'         => ",
对于此代码:

\'category_name\'         => \'\',
这将修复代码中的语法错误。

SO网友:Pat J

你说它和

$language_sql_param = \'us,gb,ca,au,ie\'

但它与

$language_sql_param = "\'de,ch\'"

它与$language_sql_param = \'de,ch\'?

理由:

\'us,gb,ca,au,ie\' != "\'us,gb,ca,ie\'". 在后一种情况下,你有额外的\' 要传递给的字符串中的字符$language_sql_param.

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post