WP Query Returning All Posts

时间:2012-06-25 作者:Ty Morton

我有一个自定义的WP查询,它返回每个帖子,而不仅仅是查询指定的帖子。我似乎找不到代码有什么问题,所以我肯定遗漏了什么:

//Process incoming variable
if(!empty($_REQUEST[\'region\'])){
     $region = $_REQUEST[\'region\'];
 } else {
     $region = NULL;
 }
if (empty($region)) {
    echo "No region selected";

//Search Region table to retrieve array of country names
}else{
    $regionresult = mysql_query("SELECT * FROM gallery_regions WHERE region=\'$region\'") or die(mysql_error());
    $num_rows = mysql_num_rows($regionresult);
    if (!$regionresult) {
    $message  = \'Invalid query: \' . mysql_error() . "\\n";
    $message .= \'Whole query: \' . $query;
    die($message);
}

// Process search reults and format array contents for WP Query
$country_search_array = array();
while($row = mysql_fetch_array($regionresult)){
    $country_search_array[] = $row[\'country\']; 
}
$country_search = "\'".implode("\',\'", $country_search_array)."\'";

//Build WP query    
$args = array(
    \'posts_per_page\' => \'-1\',
    \'cat\' => \'4\',
    \'meta_query\' => array(
        array(
        \'meta_key\' => \'Country-2\',
        \'meta_value\' => $country_search,
        \'compare\' => \'=\'
        )
    )
);
$country_query = new WP_Query( $args );

//Some output for troubleshooting purposes
$num = $country_query->post_count;
print_r ($country_search); echo "<br><br>";
print_r ($num);echo " Posts Returned.<br><br>";

// The Loop
while ( $country_query->have_posts() ) : $country_query->the_post();
    the_title(); echo"<br>";
endwhile;

// Reset Everything
wp_reset_postdata();
wp_reset_query();
mysql_free_result($regionresult);
如果这看起来很熟悉,那是因为我已经发布了与此项目相关的其他问题,所以如果这看起来重复,我很抱歉。不过,这是最后一个障碍。

一如既往,我们非常感谢您的帮助。

谢谢

更新-这是实际查询,因为它是由print\\r生成的:

WP_Query Object
(
    [query_vars] => Array
        (
            [posts_per_page] => -1
            [cat] => 4
            [meta_query] => Array
                (
                    [0] => Array
                        (
                            [meta_key] => Country
                            [meta_value] => \'Algeria\',\'Angola\',\'Benin\',\'Botswana\',\'Burkina Faso\',\'Burundi\',\'Cape Verde\',\'Central African Republic\',\'Chad\',\'Comoros\',\'Congo\',\'Ivory Coast\',\'Djibouti\',\'Egypt\',\'Eritrea\',\'Ethiopia\',\'Gabon\',\'Gambia\',\'Ghana\',\'Kenya\',\'Lesotho\',\'Liberia\',\'Libya\',\'Madagascar\',\'Malawi\',\'Mauritania\',\'Mauritius\',\'Morocco\',\'Mozambique\',\'Nambia\',\'Niger\',\'Nigeria\',\'Reunion\',\'Rwanda\',\'Sao Tome & Principe\',\'Senegal\',\'Seychelles\',\'Sierra Leone\',\'Somalia\',\'South Africa\',\'Sudan\',\'Swaziland\',\'Tanzania\',\'Togo\',\'Tunisia\',\'Uganda\',\'Zaire\',\'Zambia\',\'Zimbabwe\'
                            [compare] => IN
                        )

                )

            [error] => 
            [m] => 0
            [p] => 0
            [post_parent] => 
            [subpost] => 
            [subpost_id] => 
            [attachment] => 
            [attachment_id] => 0
            [name] => 
            [static] => 
            [pagename] => 
            [page_id] => 0
            [second] => 
            [minute] => 
            [hour] => 
            [day] => 0
            [monthnum] => 0
            [year] => 0
            [w] => 0
            [category_name] => gallery
            [tag] => 
            [tag_id] => 
            [author_name] => 
            [feed] => 
            [tb] => 
            [paged] => 0
            [comments_popup] => 
            [meta_key] => 
            [meta_value] => 
            [preview] => 
            [s] => 
            [sentence] => 
            [fields] => 
            [category__in] => Array
                (
                    [0] => 4
                )

            [category__not_in] => Array
                (
                )

            [category__and] => Array
                (
                )

            [post__in] => Array
                (
                )

            [post__not_in] => Array
                (
                )

            [tag__in] => Array
                (
                )

            [tag__not_in] => Array
                (
                )

            [tag__and] => Array
                (
                )

            [tag_slug__in] => Array
                (
                )

            [tag_slug__and] => Array
                (
                )

            [ignore_sticky_posts] => 
            [suppress_filters] => 
            [cache_results] => 1
            [update_post_term_cache] => 1
            [update_post_meta_cache] => 1
            [post_type] => 
            [nopaging] => 1
            [comments_per_page] => 50
            [no_found_rows] => 
            [order] => DESC
        )

    [tax_query] => WP_Tax_Query Object
        (
            [queries] => Array
                (
                    [0] => Array
                        (
                            [taxonomy] => category
                            [terms] => Array
                                (
                                    [0] => 4
                                )

                            [include_children] => 
                            [field] => term_id
                            [operator] => IN
                        )

                )

            [relation] => AND
        )

    [meta_query] => WP_Meta_Query Object
        (
            [queries] => Array
                (
                    [0] => Array
                        (
                            [meta_key] => Country
                            [meta_value] => \'Algeria\',\'Angola\',\'Benin\',\'Botswana\',\'Burkina Faso\',\'Burundi\',\'Cape Verde\',\'Central African Republic\',\'Chad\',\'Comoros\',\'Congo\',\'Ivory Coast\',\'Djibouti\',\'Egypt\',\'Eritrea\',\'Ethiopia\',\'Gabon\',\'Gambia\',\'Ghana\',\'Kenya\',\'Lesotho\',\'Liberia\',\'Libya\',\'Madagascar\',\'Malawi\',\'Mauritania\',\'Mauritius\',\'Morocco\',\'Mozambique\',\'Nambia\',\'Niger\',\'Nigeria\',\'Reunion\',\'Rwanda\',\'Sao Tome & Principe\',\'Senegal\',\'Seychelles\',\'Sierra Leone\',\'Somalia\',\'South Africa\',\'Sudan\',\'Swaziland\',\'Tanzania\',\'Togo\',\'Tunisia\',\'Uganda\',\'Zaire\',\'Zambia\',\'Zimbabwe\'
                            [compare] => IN
                        )

                )

            [relation] => AND
        )

    [post_count] => 1047
    [current_post] => -1
    [in_the_loop] => 
    [comment_count] => 0
    [current_comment] => -1
    [found_posts] => 0
    [max_num_pages] => 0
    [max_num_comment_pages] => 0
    [is_single] => 
    [is_preview] => 
    [is_page] => 
    [is_archive] => 1
    [is_date] => 
    [is_year] => 
    [is_month] => 
    [is_day] => 
    [is_time] => 
    [is_author] => 
    [is_category] => 1
    [is_tag] => 
    [is_tax] => 
    [is_search] => 
    [is_feed] => 
    [is_comment_feed] => 
    [is_trackback] => 
    [is_home] => 
    [is_404] => 
    [is_comments_popup] => 
    [is_paged] => 
    [is_admin] => 
    [is_attachment] => 
    [is_singular] => 
    [is_robots] => 
    [is_posts_page] => 
    [is_post_type_archive] => 
    [query_vars_hash] => 859f71690a2d92bf36791d7332bf2dbc
    [query_vars_changed] => 
    [thumbnails_cached] => 
    [query] => Array
        (
            [posts_per_page] => -1
            [cat] => 4
            [meta_query] => Array
                (
                    [0] => Array
                        (
                            [meta_key] => Country
                            [meta_value] => \'Algeria\',\'Angola\',\'Benin\',\'Botswana\',\'Burkina Faso\',\'Burundi\',\'Cape Verde\',\'Central African Republic\',\'Chad\',\'Comoros\',\'Congo\',\'Ivory Coast\',\'Djibouti\',\'Egypt\',\'Eritrea\',\'Ethiopia\',\'Gabon\',\'Gambia\',\'Ghana\',\'Kenya\',\'Lesotho\',\'Liberia\',\'Libya\',\'Madagascar\',\'Malawi\',\'Mauritania\',\'Mauritius\',\'Morocco\',\'Mozambique\',\'Nambia\',\'Niger\',\'Nigeria\',\'Reunion\',\'Rwanda\',\'Sao Tome & Principe\',\'Senegal\',\'Seychelles\',\'Sierra Leone\',\'Somalia\',\'South Africa\',\'Sudan\',\'Swaziland\',\'Tanzania\',\'Togo\',\'Tunisia\',\'Uganda\',\'Zaire\',\'Zambia\',\'Zimbabwe\'
                            [compare] => IN
                        )

                )

        )

    [request] =>  SELECT   alere_posts.* FROM alere_posts  INNER JOIN alere_term_relationships ON (alere_posts.ID = alere_term_relationships.object_id) WHERE 1=1  AND ( alere_term_relationships.term_taxonomy_id IN (4) ) AND alere_posts.post_type = \'post\' AND (alere_posts.post_status = \'publish\' OR alere_posts.post_status = \'private\') GROUP BY alere_posts.ID ORDER BY alere_posts.post_date DESC 

[posts] => Array (…

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

谢谢你的帮助!

这最终是一系列问题的结合:

而不是内爆$country_search_array, 它需要按原样添加到查询中\'=\' 用于比较值。它必须是\'IN\'

SO网友:Milo

我认为你把旧的元参数和新的元参数混淆了meta_query 参数。meta_key 应该是公正的keymeta_value 应该是公正的value. 提到WP_Query in Codex 以获取正确的语法。

而且print_r( $country_query ); 将显示发送到数据库的实际SQL查询,并显示错误所在。

结束

相关推荐

Output from Meta Box Array

我已经创建了一个元框,它成功地存储了post数据,但我在输出内容并以合理的方式显示它时遇到了困难,这超出了我对PHP的有限知识。我是一个养蜂人,我想记录我的蜂箱的检查情况,所以每个蜂箱都有一个记录,其中包含一定数量的字段。因此,我想遍历所有配置单元的colony数组,即直到$hive为空,然后以每个配置单元有一行的表的形式输出数据。目前,我已经成功地手动生成了表的第一行,但不知道如何设置一个循环来完成这项工作。<?php if (have_posts() ):?>