否则如果不工作,如果总是返回true,则选择其他选项

时间:2018-01-29 05:56:16

标签: php wordpress if-statement

我根据COUNT创建一个返回int的函数,否则如果不起作用。我可以错过什么吗?

   function kv_get_task_count() {
    global $wpdb;


            if (filter_status('new')):
                $count_post = (int) $wpdb->get_var( 'SELECT COUNT(*) FROM ' . $wpdb->prefix . 'posts
                                            WHERE post_type ="screening"
                                                AND post_status ="new"
                                            ' );
            elseif (filter_status('pending_review')): 
                $count_post =  (int) $wpdb->get_var( 'SELECT COUNT(*) FROM ' . $wpdb->prefix . 'posts
                                            WHERE post_type ="screening"
                                                AND post_status ="pending_review"
                                            ' );
            else:
                $count_post =  (int) $wpdb->get_var( 'SELECT COUNT(*) FROM ' . $wpdb->prefix . 'posts
                                            WHERE post_type ="screening"
                                                AND post_status ="new"
                                                OR post_status ="pending_review"
                                            ' );
            endif;  

            return $count_post;
}

1 个答案:

答案 0 :(得分:0)

问题关闭!!

我将代码更改为:

   function kv_get_task_count() {
    global $wpdb;


            if ($_REQUEST['post_status'] == 'new'):
                $count_post = (int) $wpdb->get_var( 'SELECT COUNT(*) FROM ' . $wpdb->prefix . 'posts
                                            WHERE post_type ="screening"
                                                AND post_status ="new"
                                            ' );
            elseif ($_REQUEST['post_status'] == 'pending_review'): 
                $count_post =  (int) $wpdb->get_var( 'SELECT COUNT(*) FROM ' . $wpdb->prefix . 'posts
                                            WHERE post_type ="screening"
                                                AND post_status ="pending_review"
                                            ' );
            else:
                $count_post =  (int) $wpdb->get_var( 'SELECT COUNT(*) FROM ' . $wpdb->prefix . 'posts
                                            WHERE post_type ="screening"
                                                AND post_status ="new"
                                                OR post_status ="pending_review"
                                            ' );
            endif;  

            return $count_post;
}