即使没有返回结果,查询计数也会返回1

时间:2013-12-12 07:35:34

标签: php sql wordpress

我正在使用WordPress。当我做一个正常的查询时,一切都很好,但当我想测试他们是否没有结果它应该显示像

"Sorry, but nothing matched your search criteria. Please try again with some different keywords."

这对我来说很奇怪,因为即使查询没有任何结果,它仍然显示为$ count 1,而它应该显示为$ count 0.这是放置的代码。

网址变量     

    $get_search = trim($get_search);
    $get_from = trim($get_from);
    $get_to = trim($get_to);
    $get_districts = trim($get_districts);
    $get_string = trim($get_string);

        if($get_search == "Submit"){

        }
}
?>

这实际上是代码

<?php
// Get districts from database plugin
    $districts = get_district();
    $options="";
        foreach($districts[0]['choices'] as $key => $val){
            if($key == $get_districts){
                $options .= "<option value='".$key."' selected>".$val."</option>";
            } else {
                $options .= "<option value='".$key."'>".$val."</option>";
            }
        }
?>
    <form method="get" action="<?php echo site_url();?>/planned-outages/" enctype="multipart/form-data">
        From Date : <input type="text" name="from" class="from" value="<?php echo $get_from; ?>"/>
        To Date : <input type="text" name="to" class="to" value="<?php echo $get_to; ?>"/>
        District: <select name="districts"><?php echo $options; ?></select>
        Search for : <input type="text" name="string" value="<?php echo $get_string; ?>"/>
        <input name="search" type="submit"/>
    </form>
<?php                   
    global $post;
    $args = array(
        'order' => 'DESC',
        'orderby' => 'post_date',
        'post_status' => 'publish',
        'post_type' => 'outages',
        's' => $get_string,
            'meta_query' => array(
                'relation' => 'OR',
                   array(
                       'key' => 'district',
                       'value' => $get_districts,
                       'compare' => 'LIKE'
                   ),
                   array(
                       'key' => 'from_date_time',
                       'value' => $get_from,
                       'compare' => 'LIKE'
                   ),
                   array(
                       'key' => 'to_date_time',
                       'value' => $get_to,
                       'compare' => 'LIKE'
                   )
            )
    );
?>  
<?php
    $list_of_posts = new WP_Query($args);
    $count = count($list_of_posts);

    if($count > 0){
        while ($list_of_posts->have_posts()):$list_of_posts->the_post();
        $meta = get_post_meta( get_the_ID(), '');
    ?>
        <?php the_title(); ?>
        <?php the_content(); ?>
    <?php
        endwhile; 
    } else {
    ?>
        Sorry, but nothing matched your search criteria. Please try again with some different keywords.
    <?php
    }
    ?>

当我var_dump($ list_of_posts)$ list_of_posts这就是我得到的

object(WP_Query)#443 (46) { ["query"]=> array(6) { ["order"]=> string(4) "DESC" ["orderby"]=> string(9) "post_date" ["post_status"]=> string(7) "publish" ["post_type"]=> string(7) "outages" ["s"]=> string(5) "links" ["meta_query"]=> array(4) { ["relation"]=> string(2) "OR" [0]=> array(3) { ["key"]=> string(8) "district" ["value"]=> string(8) "rakiraki" ["compare"]=> string(4) "LIKE" } [1]=> array(3) { ["key"]=> string(14) "from_date_time" ["value"]=> string(10) "12/26/2013" ["compare"]=> string(4) "LIKE" } [2]=> array(3) { ["key"]=> string(12) "to_date_time" ["value"]=> string(0) "" ["compare"]=> string(4) "LIKE" } } } ["query_vars"]=> array(67) { ["order"]=> string(4) "DESC" ["orderby"]=> string(9) "post_date" ["post_status"]=> string(7) "publish" ["post_type"]=> string(7) "outages" ["s"]=> string(5) "links" ["meta_query"]=> array(4) { ["relation"]=> string(2) "OR" [0]=> array(3) { ["key"]=> string(8) "district" ["value"]=> string(8) "rakiraki" ["compare"]=> string(4) "LIKE" } [1]=> array(3) { ["key"]=> string(14) "from_date_time" ["value"]=> string(10) "12/26/2013" ["compare"]=> string(4) "LIKE" } [2]=> array(3) { ["key"]=> string(12) "to_date_time" ["value"]=> string(0) "" ["compare"]=> string(4) "LIKE" } } ["error"]=> string(0) "" ["m"]=> string(0) "" ["p"]=> int(0) ["post_parent"]=> string(0) "" ["subpost"]=> string(0) "" ["subpost_id"]=> string(0) "" ["attachment"]=> string(0) "" ["attachment_id"]=> int(0) ["name"]=> string(0) "" ["static"]=> string(0) "" ["pagename"]=> string(0) "" ["page_id"]=> int(0) ["second"]=> string(0) "" ["minute"]=> string(0) "" ["hour"]=> string(0) "" ["day"]=> int(0) ["monthnum"]=> int(0) ["year"]=> int(0) ["w"]=> int(0) ["category_name"]=> string(0) "" ["tag"]=> string(0) "" ["cat"]=> string(0) "" ["tag_id"]=> string(0) "" ["author"]=> string(0) "" ["author_name"]=> string(0) "" ["feed"]=> string(0) "" ["tb"]=> string(0) "" ["paged"]=> int(0) ["comments_popup"]=> string(0) "" ["meta_key"]=> string(0) "" ["meta_value"]=> string(0) "" ["preview"]=> string(0) "" ["sentence"]=> string(0) "" ["fields"]=> string(0) "" ["menu_order"]=> string(0) "" ["category__in"]=> array(0) { } ["category__not_in"]=> array(0) { } ["category__and"]=> array(0) { } ["post__in"]=> array(0) { } ["post__not_in"]=> array(0) { } ["tag__in"]=> array(0) { } ["tag__not_in"]=> array(0) { } ["tag__and"]=> array(0) { } ["tag_slug__in"]=> array(0) { } ["tag_slug__and"]=> array(0) { } ["post_parent__in"]=> array(0) { } ["post_parent__not_in"]=> array(0) { } ["author__in"]=> array(0) { } ["author__not_in"]=> array(0) { } ["ignore_sticky_posts"]=> bool(false) ["suppress_filters"]=> bool(false) ["cache_results"]=> bool(true) ["update_post_term_cache"]=> bool(true) ["update_post_meta_cache"]=> bool(true) ["posts_per_page"]=> int(10) ["nopaging"]=> bool(false) ["comments_per_page"]=> string(2) "50" ["no_found_rows"]=> bool(false) ["search_terms_count"]=> int(1) ["search_terms"]=> array(1) { [0]=> string(5) "links" } ["search_orderby_title"]=> array(1) { [0]=> string(35) "fea_posts.post_title LIKE '%links%'" } } ["tax_query"]=> object(WP_Tax_Query)#446 (2) { ["queries"]=> array(0) { } ["relation"]=> string(3) "AND" } ["meta_query"]=> object(WP_Meta_Query)#445 (2) { ["queries"]=> array(3) { [0]=> array(3) { ["key"]=> string(8) "district" ["value"]=> string(8) "rakiraki" ["compare"]=> string(4) "LIKE" } [1]=> array(3) { ["key"]=> string(14) "from_date_time" ["value"]=> string(10) "12/26/2013" ["compare"]=> string(4) "LIKE" } [2]=> array(3) { ["key"]=> string(12) "to_date_time" ["value"]=> string(0) "" ["compare"]=> string(4) "LIKE" } } ["relation"]=> string(2) "OR" } ["date_query"]=> bool(false) ["request"]=> string(767) "SELECT SQL_CALC_FOUND_ROWS fea_posts.ID FROM fea_posts INNER JOIN fea_postmeta ON (fea_posts.ID = fea_postmeta.post_id) INNER JOIN fea_postmeta AS mt1 ON (fea_posts.ID = mt1.post_id) INNER JOIN fea_postmeta AS mt2 ON (fea_posts.ID = mt2.post_id) WHERE 1=1 AND (((fea_posts.post_title LIKE '%links%') OR (fea_posts.post_content LIKE '%links%'))) AND fea_posts.post_type = 'outages' AND (fea_posts.post_status = 'publish') AND ( (fea_postmeta.meta_key = 'district' AND CAST(fea_postmeta.meta_value AS CHAR) LIKE '%rakiraki%') OR (mt1.meta_key = 'from_date_time' AND CAST(mt1.meta_value AS CHAR) LIKE '%12/26/2013%') OR (mt2.meta_key = 'to_date_time' AND CAST(mt2.meta_value AS CHAR) LIKE '%%') ) GROUP BY fea_posts.ID ORDER BY fea_posts.post_date DESC LIMIT 0, 10" ["posts"]=> array(0) { } ["post_count"]=> int(0) ["current_post"]=> int(-1) ["in_the_loop"]=> bool(false) ["comment_count"]=> int(0) ["current_comment"]=> int(-1) ["found_posts"]=> int(0) ["max_num_pages"]=> int(0) ["max_num_comment_pages"]=> int(0) ["is_single"]=> bool(false) ["is_preview"]=> bool(false) ["is_page"]=> bool(false) ["is_archive"]=> bool(false) ["is_date"]=> bool(false) ["is_year"]=> bool(false) ["is_month"]=> bool(false) ["is_day"]=> bool(false) ["is_time"]=> bool(false) ["is_author"]=> bool(false) ["is_category"]=> bool(false) ["is_tag"]=> bool(false) ["is_tax"]=> bool(false) ["is_search"]=> bool(true) ["is_feed"]=> bool(false) ["is_comment_feed"]=> bool(false) ["is_trackback"]=> bool(false) ["is_home"]=> bool(false) ["is_404"]=> bool(false) ["is_comments_popup"]=> bool(false) ["is_paged"]=> bool(false) ["is_admin"]=> bool(false) ["is_attachment"]=> bool(false) ["is_singular"]=> bool(false) ["is_robots"]=> bool(false) ["is_posts_page"]=> bool(false) ["is_post_type_archive"]=> bool(false) ["query_vars_hash"]=> string(32) "53d948b3d27e923c345546711f9b2d03" ["query_vars_changed"]=> bool(false) ["thumbnails_cached"]=> bool(false) ["stopwords":"WP_Query":private]=> array(29) { [0]=> string(5) "about" [1]=> string(2) "an" [2]=> string(3) "are" [3]=> string(2) "as" [4]=> string(2) "at" [5]=> string(2) "be" [6]=> string(2) "by" [7]=> string(3) "com" [8]=> string(3) "for" [9]=> string(4) "from" [10]=> string(3) "how" [11]=> string(2) "in" [12]=> string(2) "is" [13]=> string(2) "it" [14]=> string(2) "of" [15]=> string(2) "on" [16]=> string(2) "or" [17]=> string(4) "that" [18]=> string(3) "the" [19]=> string(4) "this" [20]=> string(2) "to" [21]=> string(3) "was" [22]=> string(4) "what" [23]=> string(4) "when" [24]=> string(5) "where" [25]=> string(3) "who" [26]=> string(4) "will" [27]=> string(4) "with" [28]=> string(3) "www" } }

1 个答案:

答案 0 :(得分:0)

新的WP_Query($ args)只是一个Object,现在是一个数组,所以count(object)总是1。

引用WP文件:

$the_query = new WP_Query ( $args );

// The Loop
if ($the_query->have_posts ())
{
    .....
}
else
{
    // no posts found
}

http://codex.wordpress.org/Class_Reference/WP_Query

此外,您可以使用$ the_query-&gt; found_posts来获取总数。

相关问题